blob: e5fc8580d4612b7dac6c70ad341726bd531fc0de [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 Welte36158492011-02-09 21:42:34 +010024-export([parse_isup_msg/1, encode_isup_msg/1, parse_isup_party/2, encode_isup_party/1]).
Harald Welte50a44c22011-01-15 21:39:20 +010025
Harald Welte01f8ea32011-01-17 21:30:42 +010026-compile(export_all).
27
Harald Welte52725272011-04-02 16:48:50 +020028-compile({parse_transform, exprecs}).
29-export_records([party_number, isup_msg]).
30
Harald Weltede30a872011-01-16 17:12:56 +010031parse_isup_party(<<>>, OddEven, DigitList) ->
32 % in case of odd number of digits, we need to cut the last
33 case OddEven of
34 1 ->
Harald Weltec8d06c42011-02-09 22:27:19 +010035 lists:sublist(DigitList, length(DigitList)-1);
Harald Weltede30a872011-01-16 17:12:56 +010036 0 ->
Harald Weltec8d06c42011-02-09 22:27:19 +010037 DigitList
38 end;
Harald Weltede30a872011-01-16 17:12:56 +010039parse_isup_party(BcdBin, OddEven, DigitList) ->
40 <<Second:4, First:4, Remain/binary>> = BcdBin,
41 NewDigits = [First, Second],
42 parse_isup_party(Remain, OddEven, DigitList ++ NewDigits).
43
44parse_isup_party(BinBcd, OddEven) when is_binary(BinBcd) ->
45 parse_isup_party(BinBcd, OddEven, []).
46
47
48% parse a single option
Harald Welte01f8ea32011-01-17 21:30:42 +010049parse_isup_opt(OptType = ?ISUP_PAR_CALLED_P_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010050 % C.3.7 Called Party Number
51 <<OddEven:1, Nature:7, Inn:1, NumPlan:3, 0:4, Remain/binary>> = Content,
52 PhoneNum = parse_isup_party(Remain, OddEven),
53 {OptType, #party_number{nature_of_addr_ind = Nature,
54 internal_net_num = Inn,
55 numbering_plan = NumPlan,
56 phone_number = PhoneNum}};
Harald Welte01f8ea32011-01-17 21:30:42 +010057parse_isup_opt(OptType = ?ISUP_PAR_CALLING_P_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010058 % C.3.8 Calling Party Number
59 <<OddEven:1, Nature:7, Ni:1, NumPlan:3, PresRestr:2, Screen:2, Remain/binary>> = Content,
60 PhoneNum = parse_isup_party(Remain, OddEven),
61 {OptType, #party_number{nature_of_addr_ind = Nature,
62 number_incompl_ind = Ni,
63 numbering_plan = NumPlan,
64 present_restrict = PresRestr,
65 screening_ind = Screen,
66 phone_number = PhoneNum}};
Harald Welte01f8ea32011-01-17 21:30:42 +010067parse_isup_opt(OptType = ?ISUP_PAR_CONNECTED_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010068 % C.3.14 Connected Number
69 <<OddEven:1, Nature:7, 0:1, NumPlan:3, PresRestr:2, Screen:2, Remain/binary>> = Content,
70 PhoneNum = parse_isup_party(Remain, OddEven),
71 {OptType, #party_number{nature_of_addr_ind = Nature,
72 numbering_plan = NumPlan,
73 present_restrict = PresRestr,
74 screening_ind = Screen,
75 phone_number = PhoneNum}};
Harald Welte01f8ea32011-01-17 21:30:42 +010076parse_isup_opt(OptType = ?ISUP_PAR_SUBSEQ_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010077 % C.3.32 Subsequent Number
Harald Welte01f8ea32011-01-17 21:30:42 +010078 <<OddEven:1, 0:7, Remain/binary>> = Content,
Harald Weltede30a872011-01-16 17:12:56 +010079 PhoneNum = parse_isup_party(Remain, OddEven),
80 {OptType, #party_number{phone_number = PhoneNum}};
81parse_isup_opt(OptType, OptLen, Content) ->
82 {OptType, {OptLen, Content}}.
83
84% parse a Binary into a list of options
85parse_isup_opts(<<>>, OptList) ->
86 % empty list
87 OptList;
88parse_isup_opts(<<0>>, OptList) ->
89 % end of options
90 OptList;
91parse_isup_opts(OptBin, OptList) when is_binary(OptBin) ->
92 <<OptType:8, OptLen:8, Content:OptLen/binary, Remain/binary>> = OptBin,
93 NewOpt = parse_isup_opt(OptType, OptLen, Content),
Harald Welte01f8ea32011-01-17 21:30:42 +010094 parse_isup_opts(Remain, OptList ++ [NewOpt]).
95parse_isup_opts(OptBin) ->
96 parse_isup_opts(OptBin, []).
Harald Weltede30a872011-01-16 17:12:56 +010097
Harald Weltef48736b2011-01-21 14:34:32 +010098% Parse options preceeded by 1 byte OptPtr
99parse_isup_opts_ptr(OptBinPtr) ->
100 OptPtr = binary:at(OptBinPtr, 0),
101 case OptPtr of
102 0 ->
103 [];
104 _ ->
105 OptBin = binary:part(OptBinPtr, OptPtr, byte_size(OptBinPtr)-OptPtr),
106 parse_isup_opts(OptBin, [])
107 end.
108
Harald Welte50a44c22011-01-15 21:39:20 +0100109% References to 'Tabe C-xxx' are to Annex C of Q.767
110
111% Default case: no fixed and no variable parts, only options
112% ANM, RLC, FOT
113parse_isup_msgt(M, Bin) when
114 M == ?ISUP_MSGT_ANM;
115 M == ?ISUP_MSGT_RLC;
Harald Weltede30a872011-01-16 17:12:56 +0100116 M == ?ISUP_MSGT_FOT ->
Harald Weltef48736b2011-01-21 14:34:32 +0100117 parse_isup_opts_ptr(Bin);
Harald Welte50a44c22011-01-15 21:39:20 +0100118% Table C-5 Address complete
119parse_isup_msgt(?ISUP_MSGT_ACM, Bin) ->
120 <<BackCallInd:16, Remain/binary>> = Bin,
121 BciOpt = {backward_call_ind, BackCallInd},
Harald Weltef48736b2011-01-21 14:34:32 +0100122 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100123 [BciOpt|Opts];
124% Table C-7 Call progress
125parse_isup_msgt(?ISUP_MSGT_CPG, Bin) ->
126 <<EventInf:8, Remain/binary>> = Bin,
127 BciOpt = {event_info, EventInf},
Harald Weltef48736b2011-01-21 14:34:32 +0100128 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100129 [BciOpt|Opts];
130% Table C-9 Circuit group reset acknowledgement
131parse_isup_msgt(?ISUP_MSGT_GRA, Bin) ->
132 % V: Range and status
Harald Welte661e3812011-03-10 10:22:04 +0100133 <<PtrVar:8, _Remain/binary>> = Bin,
134 RangStsLen = binary:at(Bin, PtrVar),
135 RangeStatus = binary:part(Bin, PtrVar+1, RangStsLen),
Harald Welte01f8ea32011-01-17 21:30:42 +0100136 RangeStsTuple = {?ISUP_PAR_RANGE_AND_STATUS, {RangStsLen, RangeStatus}},
137 [RangeStsTuple];
Harald Welte50a44c22011-01-15 21:39:20 +0100138% Table C-11 Connect
139parse_isup_msgt(?ISUP_MSGT_CON, Bin) ->
140 <<BackCallInd:16, Remain/binary>> = Bin,
141 BciOpt = {backward_call_ind, BackCallInd},
Harald Weltef48736b2011-01-21 14:34:32 +0100142 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100143 [BciOpt|Opts];
144% Table C-12 Continuity
145parse_isup_msgt(?ISUP_MSGT_COT, Bin) ->
146 <<ContInd:8>> = Bin,
147 [{continuity_ind, ContInd}];
148% Table C-16 Initial address
149parse_isup_msgt(?ISUP_MSGT_IAM, Bin) ->
150 <<CINat:8, FwCallInd:16/big, CallingCat:8, TransmReq:8, VarAndOpt/binary>> = Bin,
151 FixedOpts = [{conn_ind_nature, CINat}, {fw_call_ind, FwCallInd}, {calling_cat, CallingCat},
152 {transm_medium_req, TransmReq}],
Harald Welte01f8ea32011-01-17 21:30:42 +0100153 <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt,
Harald Welte50a44c22011-01-15 21:39:20 +0100154 % V: Called Party Number
Harald Welte01f8ea32011-01-17 21:30:42 +0100155 CalledPartyLen = binary:at(VarAndOpt, PtrVar),
156 CalledParty = binary:part(VarAndOpt, PtrVar+1, CalledPartyLen),
157 VarOpts = [parse_isup_opt(?ISUP_PAR_CALLED_P_NUM, CalledPartyLen, CalledParty)],
158 % Optional part
Harald Welte86494e82011-01-21 12:13:35 +0000159 case PtrOpt of
160 0 ->
161 Opts = [];
162 _ ->
163 Remain = binary:part(VarAndOpt, 1 + PtrOpt, byte_size(VarAndOpt)-(1+PtrOpt)),
164 Opts = parse_isup_opts(Remain)
165 end,
Harald Welte01f8ea32011-01-17 21:30:42 +0100166 FixedOpts ++ VarOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100167% Table C-17 Release
Harald Welte2a91a012011-03-11 16:28:14 +0100168% Table 26/Q.763: Confusion
169parse_isup_msgt(M, VarAndOpt) when
170 M == ?ISUP_MSGT_REL;
171 M == ?ISUP_MSGT_CFN ->
172 <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt,
Harald Welte50a44c22011-01-15 21:39:20 +0100173 % V: Cause indicators
Harald Welte2a91a012011-03-11 16:28:14 +0100174 CauseIndLen = binary:at(VarAndOpt, PtrVar),
175 CauseInd = binary:part(VarAndOpt, PtrVar+1, CauseIndLen),
Harald Welte86494e82011-01-21 12:13:35 +0000176 VarOpts = [{?ISUP_PAR_CAUSE_IND, {CauseIndLen, CauseInd}}],
177 case PtrOpt of
178 0 ->
179 Opts = [];
180 _ ->
181 Remain = binary:part(VarAndOpt, 1 + PtrOpt, byte_size(VarAndOpt)-(1+PtrOpt)),
182 Opts = parse_isup_opts(Remain)
183 end,
Harald Welte01f8ea32011-01-17 21:30:42 +0100184 VarOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100185% Table C-19 Subsequent address
Harald Welte2a91a012011-03-11 16:28:14 +0100186parse_isup_msgt(?ISUP_MSGT_SAM, VarAndOpt) ->
187 <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt,
Harald Welte50a44c22011-01-15 21:39:20 +0100188 % V: Subsequent number
Harald Welte01f8ea32011-01-17 21:30:42 +0100189 SubseqNumLen = binary:at(VarAndOpt, PtrVar),
190 SubsetNum = binary:part(VarAndOpt, PtrVar+1, SubseqNumLen),
191 VarOpts = [{?ISUP_PAR_SUBSEQ_NUM, {SubseqNumLen, SubsetNum}}],
192 Remain = binary:part(VarAndOpt, 1 + PtrOpt, byte_size(VarAndOpt)-(1+PtrOpt)),
Harald Welte50a44c22011-01-15 21:39:20 +0100193 Opts = parse_isup_opts(Remain),
Harald Welte01f8ea32011-01-17 21:30:42 +0100194 VarOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100195% Table C-21 Suspend, Resume
Harald Weltede30a872011-01-16 17:12:56 +0100196parse_isup_msgt(Msgt, Bin) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS ->
Harald Welte50a44c22011-01-15 21:39:20 +0100197 <<SuspResInd:8, Remain/binary>> = Bin,
198 FixedOpts = [{susp_res_ind, SuspResInd}],
Harald Weltef48736b2011-01-21 14:34:32 +0100199 Opts = parse_isup_opts_ptr(Remain),
Harald Welte781d98e2011-03-11 16:48:47 +0100200 FixedOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100201% Table C-23
202parse_isup_msgt(M, <<>>) when
203 M == ?ISUP_MSGT_BLO;
204 M == ?ISUP_MSGT_BLA;
205 M == ?ISUP_MSGT_CCR;
206 M == ?ISUP_MSGT_RSC;
207 M == ?ISUP_MSGT_UBL;
208 M == ?ISUP_MSGT_UBA ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100209 [];
Harald Welte50a44c22011-01-15 21:39:20 +0100210% Table C-25
211parse_isup_msgt(M, Bin) when
212 M == ?ISUP_MSGT_CGB;
213 M == ?ISUP_MSGT_CGBA;
Harald Welte01f8ea32011-01-17 21:30:42 +0100214 M == ?ISUP_MSGT_CGU;
215 M == ?ISUP_MSGT_CGUA ->
216 <<CGMsgt:8, PtrVar:8, VarBin/binary>> = Bin,
Harald Welte50a44c22011-01-15 21:39:20 +0100217 FixedOpts = [{cg_supv_msgt, CGMsgt}],
218 % V: Range and status
Harald Welte661e3812011-03-10 10:22:04 +0100219 RangStsLen = binary:at(VarBin, PtrVar-1),
220 RangeStatus = binary:part(VarBin, PtrVar, RangStsLen),
Harald Welte01f8ea32011-01-17 21:30:42 +0100221 VarOpts = [{?ISUP_PAR_RANGE_AND_STATUS, {RangStsLen, RangeStatus}}],
222 FixedOpts ++ VarOpts;
Harald Welte50a44c22011-01-15 21:39:20 +0100223% Table C-26 Circuit group reset
224parse_isup_msgt(?ISUP_MSGT_GRS, Bin) ->
Harald Welte661e3812011-03-10 10:22:04 +0100225 <<PtrVar:8, _VarBin/binary>> = Bin,
Harald Welte50a44c22011-01-15 21:39:20 +0100226 % V: Range without status
Harald Welte661e3812011-03-10 10:22:04 +0100227 RangeLen = binary:at(Bin, PtrVar),
228 Range = binary:part(Bin, PtrVar+1, RangeLen),
Harald Welte01f8ea32011-01-17 21:30:42 +0100229 [{?ISUP_PAR_RANGE_AND_STATUS, {RangeLen, Range}}].
Harald Welte50a44c22011-01-15 21:39:20 +0100230
231
Harald Welte01f8ea32011-01-17 21:30:42 +0100232parse_isup_msg(DataBin) when is_binary(DataBin) ->
233 <<Cic:12/little, 0:4, MsgType:8, Remain/binary>> = DataBin,
Harald Welte50a44c22011-01-15 21:39:20 +0100234 Opts = parse_isup_msgt(MsgType, Remain),
235 #isup_msg{cic = Cic, msg_type = MsgType, parameters = Opts}.
Harald Weltede30a872011-01-16 17:12:56 +0100236
237
Harald Welte01f8ea32011-01-17 21:30:42 +0100238% encode a phone number from a list of digits into the BCD binary sequence
Harald Weltec8d06c42011-02-09 22:27:19 +0100239encode_isup_party(BcdInt) when is_integer(BcdInt) ->
Harald Welte93b2ab52011-02-06 21:48:58 +0100240 BcdList = osmo_util:int2digit_list(BcdInt),
Harald Weltec8d06c42011-02-09 22:27:19 +0100241 encode_isup_party(BcdList);
242encode_isup_party(BcdList) when is_list(BcdList) ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100243 encode_isup_party(BcdList, <<>>, length(BcdList)).
Harald Weltede30a872011-01-16 17:12:56 +0100244encode_isup_party([], Bin, NumDigits) ->
245 case NumDigits rem 2 of
246 1 ->
247 {Bin, 1};
248 0 ->
249 {Bin, 0}
250 end;
251encode_isup_party([First,Second|BcdList], Bin, NumDigits) ->
Harald Welte84cc60d2011-01-18 18:25:53 +0100252 encode_isup_party(BcdList, <<Bin/binary, Second:4, First:4>>, NumDigits);
253encode_isup_party([Last], Bin, NumDigits) ->
254 encode_isup_party([], <<Bin/binary, 0:4, Last:4>>, NumDigits).
Harald Welte01f8ea32011-01-17 21:30:42 +0100255
Harald Weltede30a872011-01-16 17:12:56 +0100256% encode a single option
Harald Welte84cc60d2011-01-18 18:25:53 +0100257encode_isup_par(?ISUP_PAR_CALLED_P_NUM,
Harald Weltede30a872011-01-16 17:12:56 +0100258 #party_number{nature_of_addr_ind = Nature,
259 internal_net_num = Inn,
260 numbering_plan = NumPlan,
261 phone_number= PhoneNum}) ->
262 % C.3.7 Called Party Number
263 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
Harald Welte01f8ea32011-01-17 21:30:42 +0100264 <<OddEven:1, Nature:7, Inn:1, NumPlan:3, 0:4, PhoneBin/binary>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100265encode_isup_par(?ISUP_PAR_CALLING_P_NUM,
Harald Weltede30a872011-01-16 17:12:56 +0100266 #party_number{nature_of_addr_ind = Nature,
267 number_incompl_ind = Ni,
268 numbering_plan = NumPlan,
269 present_restrict = PresRestr,
270 screening_ind = Screen,
271 phone_number= PhoneNum}) ->
272 % C.3.8 Calling Party Number
273 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
274 <<OddEven:1, Nature:7, Ni:1, NumPlan:3, PresRestr:2, Screen:2, PhoneBin/binary>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100275encode_isup_par(?ISUP_PAR_CONNECTED_NUM,
Harald Weltede30a872011-01-16 17:12:56 +0100276 #party_number{nature_of_addr_ind = Nature,
277 numbering_plan = NumPlan,
278 present_restrict = PresRestr,
279 screening_ind = Screen,
280 phone_number = PhoneNum}) ->
281 % C.3.14 Connected Number
282 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
283 <<OddEven:1, Nature:7, 0:1, NumPlan:3, PresRestr:2, Screen:2, PhoneBin/binary>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100284encode_isup_par(?ISUP_PAR_SUBSEQ_NUM,
285 #party_number{phone_number = PhoneNum}) ->
286 % C.3.32 Subsequent Number
287 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
288 <<OddEven:1, 0:7, PhoneBin/binary>>;
Harald Weltef48736b2011-01-21 14:34:32 +0100289encode_isup_par(Atom, _More) when is_atom(Atom) ->
Harald Welte84cc60d2011-01-18 18:25:53 +0100290 <<>>;
291encode_isup_par(OptNum, {OptLen, Binary}) when is_binary(Binary), is_integer(OptNum), is_integer(OptLen) ->
Harald Weltede30a872011-01-16 17:12:56 +0100292 Binary.
293
Harald Welte84cc60d2011-01-18 18:25:53 +0100294% encode a single OPTIONAL parameter (TLV type), skip all others
Harald Weltef48736b2011-01-21 14:34:32 +0100295encode_isup_optpar(ParNum, _ParBody) when is_atom(ParNum) ->
Harald Welte84cc60d2011-01-18 18:25:53 +0100296 <<>>;
297encode_isup_optpar(ParNum, ParBody) ->
298 ParBin = encode_isup_par(ParNum, ParBody),
299 ParLen = byte_size(ParBin),
300 <<ParNum:8, ParLen:8, ParBin/binary>>.
301
Harald Weltef48736b2011-01-21 14:34:32 +0100302% recursive function to encode all optional parameters
Harald Welte84cc60d2011-01-18 18:25:53 +0100303encode_isup_opts([], OutBin) ->
Harald Welte39eae2d2011-03-11 16:46:09 +0100304 % terminate with end-of-options, but only if we have options
305 case OutBin of
306 <<>> ->
307 OutBin;
308 _ ->
309 <<OutBin/binary, 0:8>>
310 end;
Harald Welte84cc60d2011-01-18 18:25:53 +0100311encode_isup_opts([Opt|OptPropList], OutBin) ->
312 {OptType, OptBody} = Opt,
313 OptBin = encode_isup_optpar(OptType, OptBody),
314 encode_isup_opts(OptPropList, <<OutBin/binary, OptBin/binary>>).
315encode_isup_opts(OptPropList) ->
316 encode_isup_opts(OptPropList, <<>>).
317
318encode_isup_hdr(#isup_msg{msg_type = MsgType, cic = Cic}) ->
319 <<Cic:12/little, 0:4, MsgType:8>>.
320
Harald Welteed4c9ea2011-01-21 16:58:19 +0000321% Default case: no fixed and no variable parts, only options
322% ANM, RLC, FOT
323encode_isup_msgt(M, #isup_msg{parameters = Params}) when
324 M == ?ISUP_MSGT_ANM;
325 M == ?ISUP_MSGT_RLC;
326 M == ?ISUP_MSGT_FOT ->
Harald Welte39eae2d2011-03-11 16:46:09 +0100327 OptBin = encode_isup_opts(Params),
328 case OptBin of
329 <<>> -> PtrOpt = 0;
330 _ -> PtrOpt = 1
331 end,
332 <<PtrOpt:8, OptBin/binary>>;
Harald Weltef48736b2011-01-21 14:34:32 +0100333% Table C-5 Address complete
334encode_isup_msgt(?ISUP_MSGT_ACM, #isup_msg{parameters = Params}) ->
335 BackCallInd = proplists:get_value(backward_call_ind, Params),
336 OptBin = encode_isup_opts(Params),
337 case OptBin of
338 <<>> -> PtrOpt = 0;
339 _ -> PtrOpt = 1
340 end,
341 <<BackCallInd:16, PtrOpt:8, OptBin/binary>>;
342% Table C-7 Call progress
343encode_isup_msgt(?ISUP_MSGT_CPG, #isup_msg{parameters = Params}) ->
344 EventInf = proplists:get_value(event_info, Params),
345 OptBin = encode_isup_opts(Params),
346 case OptBin of
347 <<>> -> PtrOpt = 0;
348 _ -> PtrOpt = 1
349 end,
350 <<EventInf:8, PtrOpt:8, OptBin/binary>>;
351% Table C-9 Circuit group reset acknowledgement
352encode_isup_msgt(?ISUP_MSGT_GRA, #isup_msg{parameters = Params}) ->
353 % V: Range and status
354 {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
355 <<1:8, RangStsLen:8, RangeStatus/binary>>;
356% Table C-11 Connect
357encode_isup_msgt(?ISUP_MSGT_CON, #isup_msg{parameters = Params}) ->
358 BackCallInd = proplists:get_value(backward_call_ind, Params),
359 OptBin = encode_isup_opts(Params),
360 case OptBin of
361 <<>> -> PtrOpt = 0;
362 _ -> PtrOpt = 1
363 end,
364 <<BackCallInd:16, PtrOpt:8, OptBin/binary>>;
365% Table C-12 Continuity
366encode_isup_msgt(?ISUP_MSGT_COT, #isup_msg{parameters = Params}) ->
367 ContInd = proplists:get_value(continuity_ind, Params),
368 <<ContInd:8>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100369% Table C-16 Initial address
370encode_isup_msgt(?ISUP_MSGT_IAM, #isup_msg{parameters = Params}) ->
371 % Fixed part
372 CINat = proplists:get_value(conn_ind_nature, Params),
373 FwCallInd = proplists:get_value(fw_call_ind, Params),
374 CallingCat = proplists:get_value(calling_cat, Params),
375 TransmReq = proplists:get_value(transm_medium_req, Params),
376 PtrVar = 2, % one byte behind the PtrOpt
377 FixedBin = <<CINat:8, FwCallInd:16/big, CallingCat:8, TransmReq:8>>,
378 % V: Called Party Number
379 CalledParty = encode_isup_par(?ISUP_PAR_CALLED_P_NUM,
380 proplists:get_value(?ISUP_PAR_CALLED_P_NUM, Params)),
381 CalledPartyLen = byte_size(CalledParty),
382 % Optional part
Harald Welted1cb16f2011-01-21 15:48:34 +0000383 Params2 = proplists:delete(?ISUP_PAR_CALLED_P_NUM, Params),
384 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100385 case OptBin of
386 <<>> -> PtrOpt = 0;
387 _ -> PtrOpt = CalledPartyLen + 1 + 1 % 1 byte length, 1 byte start offset
388 end,
389 <<FixedBin/binary, PtrVar:8, PtrOpt:8, CalledPartyLen:8, CalledParty/binary, OptBin/binary>>;
390% Table C-17 Release
Harald Welte2a91a012011-03-11 16:28:14 +0100391encode_isup_msgt(Msgt, #isup_msg{parameters = Params}) when
392 Msgt == ?ISUP_MSGT_REL;
393 Msgt == ?ISUP_MSGT_CFN ->
Harald Weltef48736b2011-01-21 14:34:32 +0100394 PtrVar = 2, % one byte behind the PtrOpt
395 % V: Cause indicators
396 CauseInd = encode_isup_par(?ISUP_PAR_CAUSE_IND,
397 proplists:get_value(?ISUP_PAR_CAUSE_IND, Params)),
398 CauseIndLen = byte_size(CauseInd),
399 % Optional Part
Harald Welted1cb16f2011-01-21 15:48:34 +0000400 Params2 = proplists:delete(?ISUP_PAR_CAUSE_IND, Params),
401 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100402 case OptBin of
403 <<>> -> PtrOpt = 0;
404 _ -> PtrOpt = CauseIndLen + 1 + 1 % 1 byte length, 1 byte start offset
405 end,
406 <<PtrVar:8, PtrOpt:8, CauseIndLen:8, CauseInd/binary, OptBin/binary>>;
407% Table C-19 Subsequent address
408encode_isup_msgt(?ISUP_MSGT_SAM, #isup_msg{parameters = Params}) ->
409 PtrVar = 2, % one byte behind the PtrOpt
410 % V: Subsequent number
411 SubseqNum = encode_isup_par(?ISUP_PAR_SUBSEQ_NUM,
412 proplists:get_value(?ISUP_PAR_SUBSEQ_NUM, Params)),
413 SubseqNumLen = byte_size(SubseqNum),
414 % Optional Part
Harald Welted1cb16f2011-01-21 15:48:34 +0000415 Params2 = proplists:delete(?ISUP_PAR_SUBSEQ_NUM, Params),
416 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100417 case OptBin of
418 <<>> -> PtrOpt = 0;
419 _ -> PtrOpt = SubseqNumLen + 1 + 1 % 1 byte length, 1 byte start offset
420 end,
421 <<PtrVar:8, PtrOpt:8, SubseqNumLen:8, SubseqNum/binary, OptBin/binary>>;
422% Table C-21 Suspend, Resume
423encode_isup_msgt(Msgt, #isup_msg{parameters = Params}) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS ->
424 SuspResInd = proplists:get_value(susp_res_ind, Params),
425 OptBin = encode_isup_opts(Params),
426 case OptBin of
427 <<>> -> PtrOpt = 0;
428 _ -> PtrOpt = 1
429 end,
430 <<SuspResInd:8, PtrOpt:8, OptBin/binary>>;
431% Table C-23
432encode_isup_msgt(M, #isup_msg{}) when
433 M == ?ISUP_MSGT_BLO;
434 M == ?ISUP_MSGT_BLA;
435 M == ?ISUP_MSGT_CCR;
436 M == ?ISUP_MSGT_RSC;
437 M == ?ISUP_MSGT_UBL;
438 M == ?ISUP_MSGT_UBA ->
439 <<>>;
440% Table C-25
441encode_isup_msgt(M, #isup_msg{parameters = Params}) when
442 M == ?ISUP_MSGT_CGB;
443 M == ?ISUP_MSGT_CGBA;
444 M == ?ISUP_MSGT_CGU;
445 M == ?ISUP_MSGT_CGUA ->
446 PtrVar = 1, % one byte behind the PtrVar
447 CGMsgt = proplists:get_value(cg_supv_msgt, Params),
448 % V: Range and status
449 {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
450 <<CGMsgt:8, PtrVar:8, RangStsLen:8, RangeStatus/binary>>;
451% Table C-26 Circuit group reset
452encode_isup_msgt(?ISUP_MSGT_GRS, #isup_msg{parameters = Params}) ->
453 PtrVar = 1, % one byte behind the PtrVar
454 {RangeLen, Range} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
455 % V: Range without status
456 <<PtrVar:8, RangeLen:8, Range/binary>>.
Harald Welte84cc60d2011-01-18 18:25:53 +0100457
458encode_isup_msg(Msg = #isup_msg{msg_type = MsgType}) ->
459 HdrBin = encode_isup_hdr(Msg),
460 Remain = encode_isup_msgt(MsgType, Msg),
461 <<HdrBin/binary, Remain/binary>>.