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 | |
Harald Welte | 3615849 | 2011-02-09 21:42:34 +0100 | [diff] [blame] | 24 | -export([parse_isup_msg/1, encode_isup_msg/1, parse_isup_party/2, encode_isup_party/1]). |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 25 | |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 26 | -compile(export_all). |
| 27 | |
Harald Welte | 5272527 | 2011-04-02 16:48:50 +0200 | [diff] [blame] | 28 | -compile({parse_transform, exprecs}). |
| 29 | -export_records([party_number, isup_msg]). |
| 30 | |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 31 | parse_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 Welte | c8d06c4 | 2011-02-09 22:27:19 +0100 | [diff] [blame] | 35 | lists:sublist(DigitList, length(DigitList)-1); |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 36 | 0 -> |
Harald Welte | c8d06c4 | 2011-02-09 22:27:19 +0100 | [diff] [blame] | 37 | DigitList |
| 38 | end; |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 39 | parse_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 | |
| 44 | parse_isup_party(BinBcd, OddEven) when is_binary(BinBcd) -> |
| 45 | parse_isup_party(BinBcd, OddEven, []). |
| 46 | |
| 47 | |
| 48 | % parse a single option |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 49 | parse_isup_opt(OptType = ?ISUP_PAR_CALLED_P_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 50 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 57 | parse_isup_opt(OptType = ?ISUP_PAR_CALLING_P_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 58 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 67 | parse_isup_opt(OptType = ?ISUP_PAR_CONNECTED_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 68 | % 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 76 | parse_isup_opt(OptType = ?ISUP_PAR_SUBSEQ_NUM, _OptLen, Content) -> |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 77 | % C.3.32 Subsequent Number |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 78 | <<OddEven:1, 0:7, Remain/binary>> = Content, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 79 | PhoneNum = parse_isup_party(Remain, OddEven), |
| 80 | {OptType, #party_number{phone_number = PhoneNum}}; |
| 81 | parse_isup_opt(OptType, OptLen, Content) -> |
| 82 | {OptType, {OptLen, Content}}. |
| 83 | |
| 84 | % parse a Binary into a list of options |
| 85 | parse_isup_opts(<<>>, OptList) -> |
| 86 | % empty list |
| 87 | OptList; |
| 88 | parse_isup_opts(<<0>>, OptList) -> |
| 89 | % end of options |
| 90 | OptList; |
| 91 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 94 | parse_isup_opts(Remain, OptList ++ [NewOpt]). |
| 95 | parse_isup_opts(OptBin) -> |
| 96 | parse_isup_opts(OptBin, []). |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 97 | |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 98 | % Parse options preceeded by 1 byte OptPtr |
| 99 | parse_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 Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 109 | % 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 |
| 113 | parse_isup_msgt(M, Bin) when |
| 114 | M == ?ISUP_MSGT_ANM; |
| 115 | M == ?ISUP_MSGT_RLC; |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 116 | M == ?ISUP_MSGT_FOT -> |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 117 | parse_isup_opts_ptr(Bin); |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 118 | % Table C-5 Address complete |
| 119 | parse_isup_msgt(?ISUP_MSGT_ACM, Bin) -> |
| 120 | <<BackCallInd:16, Remain/binary>> = Bin, |
| 121 | BciOpt = {backward_call_ind, BackCallInd}, |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 122 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 123 | [BciOpt|Opts]; |
| 124 | % Table C-7 Call progress |
| 125 | parse_isup_msgt(?ISUP_MSGT_CPG, Bin) -> |
| 126 | <<EventInf:8, Remain/binary>> = Bin, |
| 127 | BciOpt = {event_info, EventInf}, |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 128 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 129 | [BciOpt|Opts]; |
| 130 | % Table C-9 Circuit group reset acknowledgement |
| 131 | parse_isup_msgt(?ISUP_MSGT_GRA, Bin) -> |
| 132 | % V: Range and status |
Harald Welte | 661e381 | 2011-03-10 10:22:04 +0100 | [diff] [blame] | 133 | <<PtrVar:8, _Remain/binary>> = Bin, |
| 134 | RangStsLen = binary:at(Bin, PtrVar), |
| 135 | RangeStatus = binary:part(Bin, PtrVar+1, RangStsLen), |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 136 | RangeStsTuple = {?ISUP_PAR_RANGE_AND_STATUS, {RangStsLen, RangeStatus}}, |
| 137 | [RangeStsTuple]; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 138 | % Table C-11 Connect |
| 139 | parse_isup_msgt(?ISUP_MSGT_CON, Bin) -> |
| 140 | <<BackCallInd:16, Remain/binary>> = Bin, |
| 141 | BciOpt = {backward_call_ind, BackCallInd}, |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 142 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 143 | [BciOpt|Opts]; |
| 144 | % Table C-12 Continuity |
| 145 | parse_isup_msgt(?ISUP_MSGT_COT, Bin) -> |
| 146 | <<ContInd:8>> = Bin, |
| 147 | [{continuity_ind, ContInd}]; |
| 148 | % Table C-16 Initial address |
| 149 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 153 | <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 154 | % V: Called Party Number |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 155 | 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 Welte | 86494e8 | 2011-01-21 12:13:35 +0000 | [diff] [blame] | 159 | 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 166 | FixedOpts ++ VarOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 167 | % Table C-17 Release |
Harald Welte | 2a91a01 | 2011-03-11 16:28:14 +0100 | [diff] [blame] | 168 | % Table 26/Q.763: Confusion |
| 169 | parse_isup_msgt(M, VarAndOpt) when |
| 170 | M == ?ISUP_MSGT_REL; |
| 171 | M == ?ISUP_MSGT_CFN -> |
| 172 | <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 173 | % V: Cause indicators |
Harald Welte | 2a91a01 | 2011-03-11 16:28:14 +0100 | [diff] [blame] | 174 | CauseIndLen = binary:at(VarAndOpt, PtrVar), |
| 175 | CauseInd = binary:part(VarAndOpt, PtrVar+1, CauseIndLen), |
Harald Welte | 86494e8 | 2011-01-21 12:13:35 +0000 | [diff] [blame] | 176 | 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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 184 | VarOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 185 | % Table C-19 Subsequent address |
Harald Welte | 2a91a01 | 2011-03-11 16:28:14 +0100 | [diff] [blame] | 186 | parse_isup_msgt(?ISUP_MSGT_SAM, VarAndOpt) -> |
| 187 | <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 188 | % V: Subsequent number |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 189 | 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 Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 193 | Opts = parse_isup_opts(Remain), |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 194 | VarOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 195 | % Table C-21 Suspend, Resume |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 196 | 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] | 197 | <<SuspResInd:8, Remain/binary>> = Bin, |
| 198 | FixedOpts = [{susp_res_ind, SuspResInd}], |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 199 | Opts = parse_isup_opts_ptr(Remain), |
Harald Welte | 781d98e | 2011-03-11 16:48:47 +0100 | [diff] [blame] | 200 | FixedOpts ++ Opts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 201 | % Table C-23 |
| 202 | parse_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 Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 209 | []; |
Harald Welte | 2695c3b | 2011-04-14 11:42:30 +0200 | [diff] [blame] | 210 | % Table 39/Q.763 messages for national use, fixed length 1 byte msgtype |
| 211 | parse_isup_msgt(M, <<>>) when |
| 212 | M == ?ISUP_MSGT_LPA; |
| 213 | M == ?ISUP_MSGT_OLM; |
| 214 | M == ?ISUP_MSGT_UCIC -> |
| 215 | []; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 216 | % Table C-25 |
| 217 | parse_isup_msgt(M, Bin) when |
| 218 | M == ?ISUP_MSGT_CGB; |
| 219 | M == ?ISUP_MSGT_CGBA; |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 220 | M == ?ISUP_MSGT_CGU; |
| 221 | M == ?ISUP_MSGT_CGUA -> |
| 222 | <<CGMsgt:8, PtrVar:8, VarBin/binary>> = Bin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 223 | FixedOpts = [{cg_supv_msgt, CGMsgt}], |
| 224 | % V: Range and status |
Harald Welte | 661e381 | 2011-03-10 10:22:04 +0100 | [diff] [blame] | 225 | RangStsLen = binary:at(VarBin, PtrVar-1), |
| 226 | RangeStatus = binary:part(VarBin, PtrVar, RangStsLen), |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 227 | VarOpts = [{?ISUP_PAR_RANGE_AND_STATUS, {RangStsLen, RangeStatus}}], |
| 228 | FixedOpts ++ VarOpts; |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 229 | % Table C-26 Circuit group reset |
| 230 | parse_isup_msgt(?ISUP_MSGT_GRS, Bin) -> |
Harald Welte | 661e381 | 2011-03-10 10:22:04 +0100 | [diff] [blame] | 231 | <<PtrVar:8, _VarBin/binary>> = Bin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 232 | % V: Range without status |
Harald Welte | 661e381 | 2011-03-10 10:22:04 +0100 | [diff] [blame] | 233 | RangeLen = binary:at(Bin, PtrVar), |
| 234 | Range = binary:part(Bin, PtrVar+1, RangeLen), |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 235 | [{?ISUP_PAR_RANGE_AND_STATUS, {RangeLen, Range}}]. |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 236 | |
| 237 | |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 238 | parse_isup_msg(DataBin) when is_binary(DataBin) -> |
| 239 | <<Cic:12/little, 0:4, MsgType:8, Remain/binary>> = DataBin, |
Harald Welte | 50a44c2 | 2011-01-15 21:39:20 +0100 | [diff] [blame] | 240 | Opts = parse_isup_msgt(MsgType, Remain), |
| 241 | #isup_msg{cic = Cic, msg_type = MsgType, parameters = Opts}. |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 242 | |
| 243 | |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 244 | % encode a phone number from a list of digits into the BCD binary sequence |
Harald Welte | c8d06c4 | 2011-02-09 22:27:19 +0100 | [diff] [blame] | 245 | encode_isup_party(BcdInt) when is_integer(BcdInt) -> |
Harald Welte | 93b2ab5 | 2011-02-06 21:48:58 +0100 | [diff] [blame] | 246 | BcdList = osmo_util:int2digit_list(BcdInt), |
Harald Welte | c8d06c4 | 2011-02-09 22:27:19 +0100 | [diff] [blame] | 247 | encode_isup_party(BcdList); |
| 248 | encode_isup_party(BcdList) when is_list(BcdList) -> |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 249 | encode_isup_party(BcdList, <<>>, length(BcdList)). |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 250 | encode_isup_party([], Bin, NumDigits) -> |
| 251 | case NumDigits rem 2 of |
| 252 | 1 -> |
| 253 | {Bin, 1}; |
| 254 | 0 -> |
| 255 | {Bin, 0} |
| 256 | end; |
| 257 | encode_isup_party([First,Second|BcdList], Bin, NumDigits) -> |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 258 | encode_isup_party(BcdList, <<Bin/binary, Second:4, First:4>>, NumDigits); |
| 259 | encode_isup_party([Last], Bin, NumDigits) -> |
| 260 | encode_isup_party([], <<Bin/binary, 0:4, Last:4>>, NumDigits). |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 261 | |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 262 | % encode a single option |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 263 | encode_isup_par(?ISUP_PAR_CALLED_P_NUM, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 264 | #party_number{nature_of_addr_ind = Nature, |
| 265 | internal_net_num = Inn, |
| 266 | numbering_plan = NumPlan, |
| 267 | phone_number= PhoneNum}) -> |
| 268 | % C.3.7 Called Party Number |
| 269 | {PhoneBin, OddEven} = encode_isup_party(PhoneNum), |
Harald Welte | 01f8ea3 | 2011-01-17 21:30:42 +0100 | [diff] [blame] | 270 | <<OddEven:1, Nature:7, Inn:1, NumPlan:3, 0:4, PhoneBin/binary>>; |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 271 | encode_isup_par(?ISUP_PAR_CALLING_P_NUM, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 272 | #party_number{nature_of_addr_ind = Nature, |
| 273 | number_incompl_ind = Ni, |
| 274 | numbering_plan = NumPlan, |
| 275 | present_restrict = PresRestr, |
| 276 | screening_ind = Screen, |
| 277 | phone_number= PhoneNum}) -> |
| 278 | % C.3.8 Calling Party Number |
| 279 | {PhoneBin, OddEven} = encode_isup_party(PhoneNum), |
| 280 | <<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] | 281 | encode_isup_par(?ISUP_PAR_CONNECTED_NUM, |
Harald Welte | de30a87 | 2011-01-16 17:12:56 +0100 | [diff] [blame] | 282 | #party_number{nature_of_addr_ind = Nature, |
| 283 | numbering_plan = NumPlan, |
| 284 | present_restrict = PresRestr, |
| 285 | screening_ind = Screen, |
| 286 | phone_number = PhoneNum}) -> |
| 287 | % C.3.14 Connected Number |
| 288 | {PhoneBin, OddEven} = encode_isup_party(PhoneNum), |
| 289 | <<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] | 290 | encode_isup_par(?ISUP_PAR_SUBSEQ_NUM, |
| 291 | #party_number{phone_number = PhoneNum}) -> |
| 292 | % C.3.32 Subsequent Number |
| 293 | {PhoneBin, OddEven} = encode_isup_party(PhoneNum), |
| 294 | <<OddEven:1, 0:7, PhoneBin/binary>>; |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 295 | encode_isup_par(Atom, _More) when is_atom(Atom) -> |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 296 | <<>>; |
| 297 | 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] | 298 | Binary. |
| 299 | |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 300 | % encode a single OPTIONAL parameter (TLV type), skip all others |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 301 | encode_isup_optpar(ParNum, _ParBody) when is_atom(ParNum) -> |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 302 | <<>>; |
| 303 | encode_isup_optpar(ParNum, ParBody) -> |
| 304 | ParBin = encode_isup_par(ParNum, ParBody), |
| 305 | ParLen = byte_size(ParBin), |
| 306 | <<ParNum:8, ParLen:8, ParBin/binary>>. |
| 307 | |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 308 | % recursive function to encode all optional parameters |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 309 | encode_isup_opts([], OutBin) -> |
Harald Welte | 39eae2d | 2011-03-11 16:46:09 +0100 | [diff] [blame] | 310 | % terminate with end-of-options, but only if we have options |
| 311 | case OutBin of |
| 312 | <<>> -> |
| 313 | OutBin; |
| 314 | _ -> |
| 315 | <<OutBin/binary, 0:8>> |
| 316 | end; |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 317 | encode_isup_opts([Opt|OptPropList], OutBin) -> |
| 318 | {OptType, OptBody} = Opt, |
| 319 | OptBin = encode_isup_optpar(OptType, OptBody), |
| 320 | encode_isup_opts(OptPropList, <<OutBin/binary, OptBin/binary>>). |
| 321 | encode_isup_opts(OptPropList) -> |
| 322 | encode_isup_opts(OptPropList, <<>>). |
| 323 | |
| 324 | encode_isup_hdr(#isup_msg{msg_type = MsgType, cic = Cic}) -> |
| 325 | <<Cic:12/little, 0:4, MsgType:8>>. |
| 326 | |
Harald Welte | ed4c9ea | 2011-01-21 16:58:19 +0000 | [diff] [blame] | 327 | % Default case: no fixed and no variable parts, only options |
| 328 | % ANM, RLC, FOT |
| 329 | encode_isup_msgt(M, #isup_msg{parameters = Params}) when |
| 330 | M == ?ISUP_MSGT_ANM; |
| 331 | M == ?ISUP_MSGT_RLC; |
| 332 | M == ?ISUP_MSGT_FOT -> |
Harald Welte | 39eae2d | 2011-03-11 16:46:09 +0100 | [diff] [blame] | 333 | OptBin = encode_isup_opts(Params), |
| 334 | case OptBin of |
| 335 | <<>> -> PtrOpt = 0; |
| 336 | _ -> PtrOpt = 1 |
| 337 | end, |
| 338 | <<PtrOpt:8, OptBin/binary>>; |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 339 | % Table C-5 Address complete |
| 340 | encode_isup_msgt(?ISUP_MSGT_ACM, #isup_msg{parameters = Params}) -> |
| 341 | BackCallInd = proplists:get_value(backward_call_ind, Params), |
| 342 | OptBin = encode_isup_opts(Params), |
| 343 | case OptBin of |
| 344 | <<>> -> PtrOpt = 0; |
| 345 | _ -> PtrOpt = 1 |
| 346 | end, |
| 347 | <<BackCallInd:16, PtrOpt:8, OptBin/binary>>; |
| 348 | % Table C-7 Call progress |
| 349 | encode_isup_msgt(?ISUP_MSGT_CPG, #isup_msg{parameters = Params}) -> |
| 350 | EventInf = proplists:get_value(event_info, Params), |
| 351 | OptBin = encode_isup_opts(Params), |
| 352 | case OptBin of |
| 353 | <<>> -> PtrOpt = 0; |
| 354 | _ -> PtrOpt = 1 |
| 355 | end, |
| 356 | <<EventInf:8, PtrOpt:8, OptBin/binary>>; |
| 357 | % Table C-9 Circuit group reset acknowledgement |
| 358 | encode_isup_msgt(?ISUP_MSGT_GRA, #isup_msg{parameters = Params}) -> |
| 359 | % V: Range and status |
| 360 | {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params), |
| 361 | <<1:8, RangStsLen:8, RangeStatus/binary>>; |
| 362 | % Table C-11 Connect |
| 363 | encode_isup_msgt(?ISUP_MSGT_CON, #isup_msg{parameters = Params}) -> |
| 364 | BackCallInd = proplists:get_value(backward_call_ind, Params), |
| 365 | OptBin = encode_isup_opts(Params), |
| 366 | case OptBin of |
| 367 | <<>> -> PtrOpt = 0; |
| 368 | _ -> PtrOpt = 1 |
| 369 | end, |
| 370 | <<BackCallInd:16, PtrOpt:8, OptBin/binary>>; |
| 371 | % Table C-12 Continuity |
| 372 | encode_isup_msgt(?ISUP_MSGT_COT, #isup_msg{parameters = Params}) -> |
| 373 | ContInd = proplists:get_value(continuity_ind, Params), |
| 374 | <<ContInd:8>>; |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 375 | % Table C-16 Initial address |
| 376 | encode_isup_msgt(?ISUP_MSGT_IAM, #isup_msg{parameters = Params}) -> |
| 377 | % Fixed part |
| 378 | CINat = proplists:get_value(conn_ind_nature, Params), |
| 379 | FwCallInd = proplists:get_value(fw_call_ind, Params), |
| 380 | CallingCat = proplists:get_value(calling_cat, Params), |
| 381 | TransmReq = proplists:get_value(transm_medium_req, Params), |
| 382 | PtrVar = 2, % one byte behind the PtrOpt |
| 383 | FixedBin = <<CINat:8, FwCallInd:16/big, CallingCat:8, TransmReq:8>>, |
| 384 | % V: Called Party Number |
| 385 | CalledParty = encode_isup_par(?ISUP_PAR_CALLED_P_NUM, |
| 386 | proplists:get_value(?ISUP_PAR_CALLED_P_NUM, Params)), |
| 387 | CalledPartyLen = byte_size(CalledParty), |
| 388 | % Optional part |
Harald Welte | d1cb16f | 2011-01-21 15:48:34 +0000 | [diff] [blame] | 389 | Params2 = proplists:delete(?ISUP_PAR_CALLED_P_NUM, Params), |
| 390 | OptBin = encode_isup_opts(Params2), |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 391 | case OptBin of |
| 392 | <<>> -> PtrOpt = 0; |
| 393 | _ -> PtrOpt = CalledPartyLen + 1 + 1 % 1 byte length, 1 byte start offset |
| 394 | end, |
| 395 | <<FixedBin/binary, PtrVar:8, PtrOpt:8, CalledPartyLen:8, CalledParty/binary, OptBin/binary>>; |
| 396 | % Table C-17 Release |
Harald Welte | 2a91a01 | 2011-03-11 16:28:14 +0100 | [diff] [blame] | 397 | encode_isup_msgt(Msgt, #isup_msg{parameters = Params}) when |
| 398 | Msgt == ?ISUP_MSGT_REL; |
| 399 | Msgt == ?ISUP_MSGT_CFN -> |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 400 | PtrVar = 2, % one byte behind the PtrOpt |
| 401 | % V: Cause indicators |
| 402 | CauseInd = encode_isup_par(?ISUP_PAR_CAUSE_IND, |
| 403 | proplists:get_value(?ISUP_PAR_CAUSE_IND, Params)), |
| 404 | CauseIndLen = byte_size(CauseInd), |
| 405 | % Optional Part |
Harald Welte | d1cb16f | 2011-01-21 15:48:34 +0000 | [diff] [blame] | 406 | Params2 = proplists:delete(?ISUP_PAR_CAUSE_IND, Params), |
| 407 | OptBin = encode_isup_opts(Params2), |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 408 | case OptBin of |
| 409 | <<>> -> PtrOpt = 0; |
| 410 | _ -> PtrOpt = CauseIndLen + 1 + 1 % 1 byte length, 1 byte start offset |
| 411 | end, |
| 412 | <<PtrVar:8, PtrOpt:8, CauseIndLen:8, CauseInd/binary, OptBin/binary>>; |
| 413 | % Table C-19 Subsequent address |
| 414 | encode_isup_msgt(?ISUP_MSGT_SAM, #isup_msg{parameters = Params}) -> |
| 415 | PtrVar = 2, % one byte behind the PtrOpt |
| 416 | % V: Subsequent number |
| 417 | SubseqNum = encode_isup_par(?ISUP_PAR_SUBSEQ_NUM, |
| 418 | proplists:get_value(?ISUP_PAR_SUBSEQ_NUM, Params)), |
| 419 | SubseqNumLen = byte_size(SubseqNum), |
| 420 | % Optional Part |
Harald Welte | d1cb16f | 2011-01-21 15:48:34 +0000 | [diff] [blame] | 421 | Params2 = proplists:delete(?ISUP_PAR_SUBSEQ_NUM, Params), |
| 422 | OptBin = encode_isup_opts(Params2), |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 423 | case OptBin of |
| 424 | <<>> -> PtrOpt = 0; |
| 425 | _ -> PtrOpt = SubseqNumLen + 1 + 1 % 1 byte length, 1 byte start offset |
| 426 | end, |
| 427 | <<PtrVar:8, PtrOpt:8, SubseqNumLen:8, SubseqNum/binary, OptBin/binary>>; |
| 428 | % Table C-21 Suspend, Resume |
| 429 | encode_isup_msgt(Msgt, #isup_msg{parameters = Params}) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS -> |
| 430 | SuspResInd = proplists:get_value(susp_res_ind, Params), |
| 431 | OptBin = encode_isup_opts(Params), |
| 432 | case OptBin of |
| 433 | <<>> -> PtrOpt = 0; |
| 434 | _ -> PtrOpt = 1 |
| 435 | end, |
| 436 | <<SuspResInd:8, PtrOpt:8, OptBin/binary>>; |
| 437 | % Table C-23 |
| 438 | encode_isup_msgt(M, #isup_msg{}) when |
| 439 | M == ?ISUP_MSGT_BLO; |
| 440 | M == ?ISUP_MSGT_BLA; |
| 441 | M == ?ISUP_MSGT_CCR; |
| 442 | M == ?ISUP_MSGT_RSC; |
| 443 | M == ?ISUP_MSGT_UBL; |
| 444 | M == ?ISUP_MSGT_UBA -> |
| 445 | <<>>; |
Harald Welte | 2695c3b | 2011-04-14 11:42:30 +0200 | [diff] [blame] | 446 | % Table 39/Q.763 (national use) |
| 447 | encode_isup_msgt(M, #isup_msg{}) when |
| 448 | M == ?ISUP_MSGT_LPA; |
| 449 | M == ?ISUP_MSGT_OLM; |
| 450 | M == ?ISUP_MSGT_UCIC -> |
| 451 | <<>>; |
Harald Welte | f48736b | 2011-01-21 14:34:32 +0100 | [diff] [blame] | 452 | % Table C-25 |
| 453 | encode_isup_msgt(M, #isup_msg{parameters = Params}) when |
| 454 | M == ?ISUP_MSGT_CGB; |
| 455 | M == ?ISUP_MSGT_CGBA; |
| 456 | M == ?ISUP_MSGT_CGU; |
| 457 | M == ?ISUP_MSGT_CGUA -> |
| 458 | PtrVar = 1, % one byte behind the PtrVar |
| 459 | CGMsgt = proplists:get_value(cg_supv_msgt, Params), |
| 460 | % V: Range and status |
| 461 | {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params), |
| 462 | <<CGMsgt:8, PtrVar:8, RangStsLen:8, RangeStatus/binary>>; |
| 463 | % Table C-26 Circuit group reset |
| 464 | encode_isup_msgt(?ISUP_MSGT_GRS, #isup_msg{parameters = Params}) -> |
| 465 | PtrVar = 1, % one byte behind the PtrVar |
| 466 | {RangeLen, Range} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params), |
| 467 | % V: Range without status |
| 468 | <<PtrVar:8, RangeLen:8, Range/binary>>. |
Harald Welte | 84cc60d | 2011-01-18 18:25:53 +0100 | [diff] [blame] | 469 | |
| 470 | encode_isup_msg(Msg = #isup_msg{msg_type = MsgType}) -> |
| 471 | HdrBin = encode_isup_hdr(Msg), |
| 472 | Remain = encode_isup_msgt(MsgType, Msg), |
| 473 | <<HdrBin/binary, Remain/binary>>. |