Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 1 | % Conversion between SUA messages and #sccp_msg{} |
| 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 | % FIXME: this currently only supports connection-less SCCP |
| 21 | |
| 22 | -module(sua_sccp_conv). |
| 23 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 24 | |
| 25 | -include("sua.hrl"). |
| 26 | -include("sccp.hrl"). |
| 27 | |
| 28 | -export([sua_to_sccp/1, sccp_to_sua/1]). |
| 29 | |
| 30 | sua_to_sccp(M=#sua_msg{msg_class = Class, msg_type = Type}) -> |
| 31 | sua_to_sccp(Class, Type, M). |
| 32 | sua_to_sccp(?SUA_MSGC_CL, ?SUA_CL_CLDT, Sua) -> |
| 33 | Params = sua_to_sccp_params(Sua), |
| 34 | #sccp_msg{msg_type = ?SCCP_MSGT_UDT, |
| 35 | parameters = Params}; |
| 36 | sua_to_sccp(?SUA_MSGC_CL, ?SUA_CL_CLDR, Sua) -> |
| 37 | Params = sua_to_sccp_params(Sua), |
| 38 | #sccp_msg{msg_type = ?SCCP_MSGT_UDTS, |
| 39 | parameters = Params}. |
| 40 | |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 41 | sccp_to_sua(#sccp_msg{msg_type = Type, parameters = Params}) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 42 | sccp_to_sua(Type, Params). |
| 43 | sccp_to_sua(Type, Params) when Type == ?SCCP_MSGT_UDT; |
| 44 | Type == ?SCCP_MSGT_XUDT; |
| 45 | Type == ?SCCP_MSGT_LUDT -> |
Harald Welte | 8e92c9a | 2012-01-18 00:25:26 +0100 | [diff] [blame] | 46 | Opts = sccp_to_sua_params(Type, Params), |
| 47 | #sua_msg{version = 1, msg_class = ?SUA_MSGC_CL, |
| 48 | msg_type = ?SUA_CL_CLDT, payload = Opts}; |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 49 | sccp_to_sua(Type, Params) when Type == ?SCCP_MSGT_UDTS; |
| 50 | Type == ?SCCP_MSGT_XUDTS; |
| 51 | Type == ?SCCP_MSGT_LUDTS -> |
| 52 | Opts = sccp_to_sua_params(Params), |
Harald Welte | 8e92c9a | 2012-01-18 00:25:26 +0100 | [diff] [blame] | 53 | #sua_msg{version=1, msg_class = ?SUA_MSGC_CL, |
| 54 | msg_type = ?SUA_CL_CLDR, payload = Opts}. |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 55 | |
| 56 | |
| 57 | % CLDT parameters: |
| 58 | % ?SUA_IEI_ROUTE_CTX, ?SUA_IEI_PROTO_CLASS, ?SUA_IEI_SRC_ADDR, |
| 59 | % ?SUA_IEI_DEST_ADDR, ?SUA_IEI_SEQ_CTRL, ?SUA_IEI_S7_HOP_CTR, |
| 60 | % ?SUA_IEI_IMPORTANCE, ?SUA_IEI_MSG_PRIO, ?SUA_IEI_CORR_ID, |
| 61 | % ?SUA_IEI_SEGMENTATION, ?SUA_IEI_DATA |
| 62 | |
| 63 | sua_to_sccp_params(#sua_msg{msg_class=Class, msg_type=Type, payload=Payload}) -> |
| 64 | sua_to_sccp_params(Class, Type, Payload). |
| 65 | sua_to_sccp_params(Class, Type, Payload) -> |
| 66 | sua_to_sccp_params(Class, Type, Payload, []). |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 67 | sua_to_sccp_params(_Class, _Type, [], List) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 68 | List; |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 69 | sua_to_sccp_params(Class, Type, [{ParTag, {_Len, ParVal}}|Remain], List) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 70 | NewPars = sua_to_sccp_param(Class, Type, ParTag, ParVal), |
| 71 | sua_to_sccp_params(Class, Type, Remain, List ++ NewPars). |
| 72 | |
| 73 | % convert an individual SUA parameter to a SCCP option |
| 74 | sua_to_sccp_param(_, _, ?SUA_IEI_PROTO_CLASS, Remain) -> |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 75 | <<_:24, PCOpt:4, _:2, Class:2>> = Remain, |
| 76 | [{protocol_class, {Class, PCOpt}}]; |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 77 | sua_to_sccp_param(_, _, ?SUA_IEI_SRC_ADDR, Remain) -> |
| 78 | Addr = sua_to_sccp_addr(Remain), |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 79 | [{calling_party_addr, Addr}]; |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 80 | sua_to_sccp_param(_, _, ?SUA_IEI_DEST_ADDR, Remain) -> |
| 81 | Addr = sua_to_sccp_addr(Remain), |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 82 | [{called_party_addr, Addr}]; |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 83 | sua_to_sccp_param(_, _, ?SUA_IEI_SEQ_CTRL, _Remain) -> |
| 84 | % If we were to translate to a N-UNITDATA.req, we could map |
| 85 | % this, but there is no mapping to a SCCP message... |
| 86 | []; |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 87 | sua_to_sccp_param(_, _, ?SUA_IEI_S7_HOP_CTR, Remain) -> |
| 88 | <<_:24, HopCtr:8>> = Remain, |
| 89 | [{?SCCP_PNC_HOP_COUNTER, HopCtr}]; |
| 90 | sua_to_sccp_param(_, _, ?SUA_IEI_IMPORTANCE, Remain) -> |
| 91 | <<_:24, Imp:8>> = Remain, |
| 92 | [{?SCCP_PNC_IMPORTANCE, Imp}]; |
| 93 | sua_to_sccp_param(_, _, ?SUA_IEI_DATA, Remain) -> |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 94 | [{user_data, Remain}]; |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 95 | sua_to_sccp_param(_, _, ?SUA_IEI_ROUTE_CTX, _Remain) -> |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 96 | %FIXME: what to do with routing context? |
| 97 | []. |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 98 | |
| 99 | sccp_to_sua_params(#sccp_msg{msg_type=Type, parameters=Params}) -> |
| 100 | sccp_to_sua_params(Type, Params). |
| 101 | sccp_to_sua_params(Type, Params) when is_list(Params) -> |
| 102 | sccp_to_sua_params(Type, Params, []). |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 103 | sccp_to_sua_params(_Type, [], List) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 104 | List; |
| 105 | sccp_to_sua_params(Type, [{ParTag, ParVal}|Tail], List) -> |
| 106 | NewPars = sccp_to_sua_param(Type, ParTag, ParVal), |
| 107 | sccp_to_sua_params(Type, Tail, List ++ NewPars). |
| 108 | |
Harald Welte | 99efaf9 | 2012-01-18 14:11:23 +0100 | [diff] [blame^] | 109 | sccp_to_sua_param(_, protocol_class, {Opt, Class}) -> |
| 110 | [{?SUA_IEI_PROTO_CLASS, <<0:24, Opt:4, 0:2, Class:2>>}]; |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 111 | sccp_to_sua_param(_, calling_party_addr, Addr) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 112 | AddrSua = sccp_to_sua_addr(Addr), |
| 113 | [{?SUA_IEI_SRC_ADDR, AddrSua}]; |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 114 | sccp_to_sua_param(_, called_party_addr, Addr) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 115 | AddrSua = sccp_to_sua_addr(Addr), |
| 116 | [{?SUA_IEI_DEST_ADDR, AddrSua}]; |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 117 | sccp_to_sua_param(_, ?SCCP_PNC_HOP_COUNTER, Hop) -> |
| 118 | [{?SUA_IEI_S7_HOP_CTR, <<0:24, Hop:8>>}]; |
| 119 | sccp_to_sua_param(_, ?SCCP_PNC_IMPORTANCE, Imp) -> |
| 120 | [{?SUA_IEI_IMPORTANCE, <<0:24, Imp:8>>}]; |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 121 | sccp_to_sua_param(_, user_data, Data) -> |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 122 | [{?SUA_IEI_DATA, Data}]. |
| 123 | |
| 124 | sua_to_sccp_addr(SuaBin) -> |
| 125 | <<RoutInd:16, _:13, GTinc:1, PCinc:1, SSNinc:1, Remain/binary>> = SuaBin, |
| 126 | ParList = addr_pars_to_list(Remain), |
| 127 | case GTinc of |
| 128 | 1 -> |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 129 | {_, GTopt} = proplists:get_value(?SUA_IEI_GT, ParList), |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 130 | GT = parse_sua_gt(GTopt); |
| 131 | 0 -> |
| 132 | GT = undefined |
| 133 | end, |
| 134 | case PCinc of |
| 135 | 1 -> |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 136 | {_, PCopt} = proplists:get_value(?SUA_IEI_PC, ParList), |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 137 | PC = parse_sua_pc(PCopt); |
| 138 | 0 -> |
| 139 | PC = undefined |
| 140 | end, |
| 141 | case SSNinc of |
| 142 | 1 -> |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 143 | {_, SSNopt} = proplists:get_value(?SUA_IEI_SSN, ParList), |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 144 | SSN = parse_sua_ssn(SSNopt); |
| 145 | 0 -> |
| 146 | SSN = undefined |
| 147 | end, |
| 148 | case RoutInd of |
| 149 | ?SUA_RI_GT -> |
| 150 | RoutSSN = 0; |
| 151 | ?SUA_RI_SSN_PC -> |
| 152 | RoutSSN = 1 |
| 153 | end, |
| 154 | #sccp_addr{route_on_ssn = RoutSSN, point_code = PC, ssn = SSN, global_title = GT}. |
| 155 | |
| 156 | addr_pars_to_list(Bin) -> |
| 157 | sua_codec:parse_xua_opts(Bin). |
| 158 | |
| 159 | sccp_to_sua_addr(Addr) when is_record(Addr, sccp_addr) -> |
| 160 | #sccp_addr{route_on_ssn = RoutOnSsn, point_code = PC, ssn = SSN, |
| 161 | global_title = GT} = Addr, |
| 162 | case GT of |
| 163 | #global_title{} -> |
Harald Welte | 8e92c9a | 2012-01-18 00:25:26 +0100 | [diff] [blame] | 164 | GTopt = [{?SUA_IEI_GT, encode_sua_gt(GT)}], |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 165 | GTinc = 1; |
| 166 | _ -> |
| 167 | GTopt = [], |
| 168 | GTinc = 0 |
| 169 | end, |
| 170 | case PC of |
| 171 | Int when is_integer(Int) -> |
Harald Welte | 8e92c9a | 2012-01-18 00:25:26 +0100 | [diff] [blame] | 172 | PCopt = [{?SUA_IEI_PC, encode_sua_pc(PC)}], |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 173 | PCinc = 1; |
| 174 | _ -> |
| 175 | PCopt = [], |
| 176 | PCinc = 0 |
| 177 | end, |
| 178 | case SSN of |
| 179 | Int2 when is_integer(Int2) -> |
Harald Welte | 8e92c9a | 2012-01-18 00:25:26 +0100 | [diff] [blame] | 180 | SSNopt = [{?SUA_IEI_SSN, encode_sua_ssn(SSN)}], |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 181 | SSNinc = 1; |
| 182 | _ -> |
| 183 | SSNopt = [], |
| 184 | SSNinc = 0 |
| 185 | end, |
| 186 | case RoutOnSsn of |
| 187 | 0 -> |
| 188 | RoutInd = ?SUA_RI_GT; |
| 189 | 1 -> |
| 190 | RoutInd = ?SUA_RI_SSN_PC |
| 191 | end, |
| 192 | Tail = sua_codec:encode_xua_opts(GTopt ++ PCopt ++ SSNopt), |
| 193 | <<RoutInd:16, 0:13, GTinc:1, PCinc:1, SSNinc:1, Tail/binary>>. |
| 194 | |
| 195 | parse_sua_gt(Bin) -> |
| 196 | <<_:24, GTI:8, NoDigits:8, TransType:8, NumPlan:8, NAI:8, Remain/binary>> = Bin, |
| 197 | Number = parse_sua_gt_digits(NoDigits, Remain), |
| 198 | #global_title{gti = GTI, nature_of_addr_ind = NAI, |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 199 | trans_type = TransType, |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 200 | numbering_plan = NumPlan, |
| 201 | phone_number = Number}. |
| 202 | encode_sua_gt(Gt) when is_record(Gt, global_title) -> |
| 203 | #global_title{gti = GTI, nature_of_addr_ind = NAI, |
Harald Welte | 7e1c261 | 2012-01-18 07:44:31 +0100 | [diff] [blame] | 204 | trans_type = TransType, |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 205 | numbering_plan = NumPlan, |
| 206 | phone_number = Number} = Gt, |
| 207 | NoDigits = count_digits(Number), |
| 208 | DigitBin = encode_sua_gt_digits(Number), |
| 209 | <<0:24, GTI:8, NoDigits:8, TransType:8, NumPlan:8, NAI:8, DigitBin/binary>>. |
| 210 | |
| 211 | count_digits(Number) when is_integer(Number) -> |
| 212 | BcdList = osmo_util:int2digit_list(Number), |
| 213 | count_digits(BcdList); |
| 214 | count_digits(Number) when is_list(Number) -> |
| 215 | length(Number). |
| 216 | |
| 217 | |
| 218 | parse_sua_gt_digits(NoDigits, Remain) -> |
| 219 | % as opposed to ISUP/SCCP, we can have more than one nibble padding, |
| 220 | OddEven = NoDigits rem 1, |
| 221 | case OddEven of |
| 222 | 0 -> |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 223 | ByteLen = NoDigits div 2; |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 224 | 1 -> |
Harald Welte | 042a579 | 2012-01-18 00:09:51 +0100 | [diff] [blame] | 225 | ByteLen = NoDigits div 2 + 1 |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 226 | end, |
| 227 | <<Bin:ByteLen/binary, _/binary>> = Remain, |
| 228 | isup_codec:parse_isup_party(Bin, OddEven). |
| 229 | encode_sua_gt_digits(Digits) when is_list(Digits); is_integer(Digits) -> |
| 230 | % Assume that overall option encoder will do the padding... |
Harald Welte | 8e92c9a | 2012-01-18 00:25:26 +0100 | [diff] [blame] | 231 | {Enc, _OddEven} = isup_codec:encode_isup_party(Digits), |
| 232 | Enc. |
Harald Welte | 50dfc19 | 2012-01-17 15:11:37 +0100 | [diff] [blame] | 233 | |
| 234 | parse_sua_pc(<<PC:32/big>>) -> |
| 235 | PC. |
| 236 | encode_sua_pc(Pc) when is_integer(Pc) -> |
| 237 | <<Pc:32/big>>. |
| 238 | |
| 239 | parse_sua_ssn(<<_:24, SSN:8>>) -> |
| 240 | SSN. |
| 241 | encode_sua_ssn(Ssn) when is_integer(Ssn) -> |
| 242 | <<0:24, Ssn:8>>. |