Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 1 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 26 | -compile(export_all). |
| 27 | |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 28 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 32 | lists:sublist(DigitList, length(DigitList)-1); |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 33 | 0 -> |
| 34 | DigitList |
| 35 | end; |
| 36 | parse_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 | |
| 41 | parse_isup_party(BinBcd, OddEven) when is_binary(BinBcd) -> |
| 42 | parse_isup_party(BinBcd, OddEven, []). |
| 43 | |
| 44 | |
| 45 | % parse a single option |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 46 | parse_isup_opt(OptType = ?ISUP_PAR_CALLED_P_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 47 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 54 | parse_isup_opt(OptType = ?ISUP_PAR_CALLING_P_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 55 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 64 | parse_isup_opt(OptType = ?ISUP_PAR_CONNECTED_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 65 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 73 | parse_isup_opt(OptType = ?ISUP_PAR_SUBSEQ_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 74 | % C.3.32 Subsequent Number |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 75 | <<OddEven:1, 0:7, Remain/binary>> = Content, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 76 | PhoneNum = parse_isup_party(Remain, OddEven), |
| 77 | {OptType, #party_number{phone_number = PhoneNum}}; |
| 78 | parse_isup_opt(OptType, OptLen, Content) -> |
| 79 | {OptType, {OptLen, Content}}. |
| 80 | |
| 81 | % parse a Binary into a list of options |
| 82 | parse_isup_opts(<<>>, OptList) -> |
| 83 | % empty list |
| 84 | OptList; |
| 85 | parse_isup_opts(<<0>>, OptList) -> |
| 86 | % end of options |
| 87 | OptList; |
| 88 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 91 | parse_isup_opts(Remain, OptList ++ [NewOpt]). |
| 92 | parse_isup_opts(OptBin) -> |
| 93 | parse_isup_opts(OptBin, []). |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 94 | |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 95 | % Parse options preceeded by 1 byte OptPtr |
| 96 | parse_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 Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 106 | % 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 |
| 110 | parse_isup_msgt(M, Bin) when |
| 111 | M == ?ISUP_MSGT_ANM; |
| 112 | M == ?ISUP_MSGT_RLC; |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 113 | M == ?ISUP_MSGT_FOT -> |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 114 | parse_isup_opts_ptr(Bin); |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 115 | % Table C-5 Address complete |
| 116 | parse_isup_msgt(?ISUP_MSGT_ACM, Bin) -> |
| 117 | <<BackCallInd:16, Remain/binary>> = Bin, |
| 118 | BciOpt = {backward_call_ind, BackCallInd}, |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 119 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 120 | [BciOpt|Opts]; |
| 121 | % Table C-7 Call progress |
| 122 | parse_isup_msgt(?ISUP_MSGT_CPG, Bin) -> |
| 123 | <<EventInf:8, Remain/binary>> = Bin, |
| 124 | BciOpt = {event_info, EventInf}, |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 125 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 126 | [BciOpt|Opts]; |
| 127 | % Table C-9 Circuit group reset acknowledgement |
| 128 | parse_isup_msgt(?ISUP_MSGT_GRA, Bin) -> |
| 129 | % V: Range and status |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 130 | <<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 Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 135 | % Table C-11 Connect |
| 136 | parse_isup_msgt(?ISUP_MSGT_CON, Bin) -> |
| 137 | <<BackCallInd:16, Remain/binary>> = Bin, |
| 138 | BciOpt = {backward_call_ind, BackCallInd}, |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 139 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 140 | [BciOpt|Opts]; |
| 141 | % Table C-12 Continuity |
| 142 | parse_isup_msgt(?ISUP_MSGT_COT, Bin) -> |
| 143 | <<ContInd:8>> = Bin, |
| 144 | [{continuity_ind, ContInd}]; |
| 145 | % Table C-16 Initial address |
| 146 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 150 | <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 151 | % V: Called Party Number |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 152 | 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 Welte | 86494e8 | 2011-01-21 12:13:35 +0000 | [diff] [blame] | 156 | 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 163 | FixedOpts ++ VarOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 164 | % Table C-17 Release |
| 165 | parse_isup_msgt(?ISUP_MSGT_REL, Bin) -> |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 166 | <<PtrVar:8, PtrOpt:8, VarAndOpt/binary>> = Bin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 167 | % V: Cause indicators |
Harald Welte | 86494e8 | 2011-01-21 12:13:35 +0000 | [diff] [blame] | 168 | 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 178 | VarOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 179 | % Table C-19 Subsequent address |
| 180 | parse_isup_msgt(?ISUP_MSGT_SAM, Bin) -> |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 181 | <<PtrVar:8, PtrOpt:8, VarAndOpt/binary>> = Bin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 182 | % V: Subsequent number |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 183 | 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 Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 187 | Opts = parse_isup_opts(Remain), |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 188 | VarOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 189 | % Table C-21 Suspend, Resume |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 190 | parse_isup_msgt(Msgt, Bin) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS -> |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 191 | <<SuspResInd:8, Remain/binary>> = Bin, |
| 192 | FixedOpts = [{susp_res_ind, SuspResInd}], |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 193 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 194 | [FixedOpts|Opts]; |
| 195 | % Table C-23 |
| 196 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 203 | []; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 204 | % Table C-25 |
| 205 | parse_isup_msgt(M, Bin) when |
| 206 | M == ?ISUP_MSGT_CGB; |
| 207 | M == ?ISUP_MSGT_CGBA; |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 208 | M == ?ISUP_MSGT_CGU; |
| 209 | M == ?ISUP_MSGT_CGUA -> |
| 210 | <<CGMsgt:8, PtrVar:8, VarBin/binary>> = Bin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 211 | FixedOpts = [{cg_supv_msgt, CGMsgt}], |
| 212 | % V: Range and status |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 213 | 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 Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 217 | % Table C-26 Circuit group reset |
| 218 | parse_isup_msgt(?ISUP_MSGT_GRS, Bin) -> |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 219 | <<PtrVar:8, VarBin/binary>> = Bin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 220 | % V: Range without status |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 221 | RangeLen = binary:at(VarBin, PtrVar), |
| 222 | Range = binary:part(VarBin, PtrVar+1, RangeLen), |
| 223 | [{?ISUP_PAR_RANGE_AND_STATUS, {RangeLen, Range}}]. |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 224 | |
| 225 | |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 226 | parse_isup_msg(DataBin) when is_binary(DataBin) -> |
| 227 | <<Cic:12/little, 0:4, MsgType:8, Remain/binary>> = DataBin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 228 | Opts = parse_isup_msgt(MsgType, Remain), |
| 229 | #isup_msg{cic = Cic, msg_type = MsgType, parameters = Opts}. |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 230 | |
| 231 | |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 232 | % encode a phone number from a list of digits into the BCD binary sequence |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 233 | encode_isup_party(BcdList) -> |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 234 | encode_isup_party(BcdList, <<>>, length(BcdList)). |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 235 | encode_isup_party([], Bin, NumDigits) -> |
| 236 | case NumDigits rem 2 of |
| 237 | 1 -> |
| 238 | {Bin, 1}; |
| 239 | 0 -> |
| 240 | {Bin, 0} |
| 241 | end; |
| 242 | encode_isup_party([First,Second|BcdList], Bin, NumDigits) -> |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 243 | encode_isup_party(BcdList, <<Bin/binary, Second:4, First:4>>, NumDigits); |
| 244 | encode_isup_party([Last], Bin, NumDigits) -> |
| 245 | encode_isup_party([], <<Bin/binary, 0:4, Last:4>>, NumDigits). |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 246 | |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 247 | % encode a single option |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 248 | encode_isup_par(?ISUP_PAR_CALLED_P_NUM, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 249 | #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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 255 | <<OddEven:1, Nature:7, Inn:1, NumPlan:3, 0:4, PhoneBin/binary>>; |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 256 | encode_isup_par(?ISUP_PAR_CALLING_P_NUM, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 257 | #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 Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 266 | encode_isup_par(?ISUP_PAR_CONNECTED_NUM, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 267 | #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 Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 275 | encode_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 Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 280 | encode_isup_par(Atom, _More) when is_atom(Atom) -> |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 281 | <<>>; |
| 282 | encode_isup_par(OptNum, {OptLen, Binary}) when is_binary(Binary), is_integer(OptNum), is_integer(OptLen) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 283 | Binary. |
| 284 | |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 285 | % encode a single OPTIONAL parameter (TLV type), skip all others |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 286 | encode_isup_optpar(ParNum, _ParBody) when is_atom(ParNum) -> |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 287 | <<>>; |
| 288 | encode_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 Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 293 | % recursive function to encode all optional parameters |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 294 | encode_isup_opts([], OutBin) -> |
Harald Welte | 548f93b | 2011-01-21 16:06:49 +0000 | [diff] [blame] | 295 | <<OutBin/binary, 0:8>>; |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 296 | encode_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>>). |
| 300 | encode_isup_opts(OptPropList) -> |
| 301 | encode_isup_opts(OptPropList, <<>>). |
| 302 | |
| 303 | encode_isup_hdr(#isup_msg{msg_type = MsgType, cic = Cic}) -> |
| 304 | <<Cic:12/little, 0:4, MsgType:8>>. |
| 305 | |
Harald Welte | ed4c9ea | 2011-01-21 16:58:19 +0000 | [diff] [blame] | 306 | % Default case: no fixed and no variable parts, only options |
| 307 | % ANM, RLC, FOT |
| 308 | encode_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 Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 313 | % Table C-5 Address complete |
| 314 | encode_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 |
| 323 | encode_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 |
| 332 | encode_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 |
| 337 | encode_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 |
| 346 | encode_isup_msgt(?ISUP_MSGT_COT, #isup_msg{parameters = Params}) -> |
| 347 | ContInd = proplists:get_value(continuity_ind, Params), |
| 348 | <<ContInd:8>>; |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 349 | % Table C-16 Initial address |
| 350 | encode_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 Welte | d1cb16f | 2011-01-21 15:48:34 +0000 | [diff] [blame] | 363 | Params2 = proplists:delete(?ISUP_PAR_CALLED_P_NUM, Params), |
| 364 | OptBin = encode_isup_opts(Params2), |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 365 | 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 |
| 371 | encode_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 Welte | d1cb16f | 2011-01-21 15:48:34 +0000 | [diff] [blame] | 378 | Params2 = proplists:delete(?ISUP_PAR_CAUSE_IND, Params), |
| 379 | OptBin = encode_isup_opts(Params2), |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 380 | 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 |
| 386 | encode_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 Welte | d1cb16f | 2011-01-21 15:48:34 +0000 | [diff] [blame] | 393 | Params2 = proplists:delete(?ISUP_PAR_SUBSEQ_NUM, Params), |
| 394 | OptBin = encode_isup_opts(Params2), |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 395 | 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 |
| 401 | encode_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 |
| 410 | encode_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 |
| 419 | encode_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 |
| 430 | encode_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 Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 435 | |
| 436 | encode_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>>. |