Harald Welte | 268c572 | 2011-02-09 14:38:15 +0100 | [diff] [blame] | 1 | % GSM MAP codec wrapper functions |
| 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(map_codec). |
| 21 | -author('Harald Welte <laforge@gnumonks.org>'). |
Harald Welte | 21c6b94 | 2011-04-16 20:14:38 +0200 | [diff] [blame^] | 22 | -include("map.hrl"). |
Harald Welte | 74368cc | 2011-02-09 21:41:36 +0100 | [diff] [blame] | 23 | -include_lib("osmo_ss7/include/isup.hrl"). |
Harald Welte | 268c572 | 2011-02-09 14:38:15 +0100 | [diff] [blame] | 24 | |
Harald Welte | 0aa140b | 2011-02-09 23:15:22 +0100 | [diff] [blame] | 25 | -export([parse_tcap_msg/1, encode_tcap_msg/1]). |
Harald Welte | 74368cc | 2011-02-09 21:41:36 +0100 | [diff] [blame] | 26 | -export([parse_addr_string/1, encode_addr_string/1]). |
Harald Welte | 268c572 | 2011-02-09 14:38:15 +0100 | [diff] [blame] | 27 | |
Harald Welte | 74368cc | 2011-02-09 21:41:36 +0100 | [diff] [blame] | 28 | -define(MAP_ADDR_NAT_UNKNOWN, 0). |
| 29 | -define(MAP_ADDR_NAT_INTERNATIONAL, 1). |
| 30 | -define(MAP_ADDR_NAT_NATIONAL, 2). |
| 31 | -define(MAP_ADDR_NAT_NETWORK_SPEC, 3). |
| 32 | -define(MAP_ADDR_NAT_SUBSCRIBER, 4). |
| 33 | -define(MAP_ADDR_NAT_RES, 5). |
| 34 | -define(MAP_ADDR_NAT_ABBREVIATED, 6). |
| 35 | -define(MAP_ADDR_NAT_RES_FOR_EXT, 7). |
| 36 | |
| 37 | % convert from MAP -> ISUP 'nature of address' |
| 38 | nature_map2isup(NatureMap) -> |
| 39 | case NatureMap of |
| 40 | ?MAP_ADDR_NAT_INTERNATIONAL -> ?ISUP_ADDR_NAT_INTERNATIONAL; |
| 41 | ?MAP_ADDR_NAT_NATIONAL -> ?ISUP_ADDR_NAT_NATIONAL; |
| 42 | ?MAP_ADDR_NAT_SUBSCRIBER -> ?ISUP_ADDR_NAT_SUBSCRIBER; |
| 43 | _ -> NatureMap |
| 44 | end. |
| 45 | |
| 46 | % convert from ISUP -> MAP 'nature of address' |
| 47 | nature_isup2map(NatureIsup) -> |
| 48 | case NatureIsup of |
| 49 | ?ISUP_ADDR_NAT_INTERNATIONAL -> ?MAP_ADDR_NAT_INTERNATIONAL; |
| 50 | ?ISUP_ADDR_NAT_NATIONAL -> ?MAP_ADDR_NAT_NATIONAL; |
| 51 | ?ISUP_ADDR_NAT_SUBSCRIBER -> ?MAP_ADDR_NAT_SUBSCRIBER; |
| 52 | _ -> NatureIsup |
| 53 | end. |
| 54 | |
Harald Welte | 4ae92fb | 2011-02-10 17:53:15 +0100 | [diff] [blame] | 55 | % Parse a TBCD-STRING |
| 56 | parse_map_tbcd(<<>>, DigitList) -> |
| 57 | DigitList; |
| 58 | parse_map_tbcd(BcdBin, DigitList) -> |
| 59 | <<Second:4, First:4, Remain/binary>> = BcdBin, |
| 60 | NewDigits = [First, Second], |
| 61 | parse_map_tbcd(Remain, DigitList ++ NewDigits). |
| 62 | parse_map_tbcd(ListBcd) when is_list(ListBcd) -> |
| 63 | BinBcd = list_to_binary(ListBcd), |
| 64 | parse_map_tbcd(BinBcd); |
| 65 | parse_map_tbcd(BinBcd) when is_binary(BinBcd) -> |
| 66 | parse_map_tbcd(BinBcd, []). |
| 67 | |
| 68 | % like parse_map_tbcd, but remove any trailing 0xF |
| 69 | parse_map_addr(Bcd) -> |
| 70 | DigitList = parse_map_tbcd(Bcd), |
| 71 | LastDigit = lists:last(DigitList), |
| 72 | if |
| 73 | LastDigit == 15 -> |
| 74 | lists:sublist(DigitList, length(DigitList)-1); |
| 75 | true -> |
| 76 | DigitList |
| 77 | end. |
| 78 | |
| 79 | encode_map_tbcd(BcdInt) when is_integer(BcdInt) -> |
| 80 | BcdList = osmo_util:int2digit_list(BcdInt), |
| 81 | encode_map_tbcd(BcdList); |
| 82 | encode_map_tbcd(BcdList) when is_list(BcdList) -> |
| 83 | encode_map_tbcd(BcdList, <<>>). |
| 84 | encode_map_tbcd([], Bin) -> |
| 85 | Bin; |
| 86 | encode_map_tbcd([First,Second|BcdList], Bin) -> |
| 87 | encode_map_tbcd(BcdList, <<Bin/binary, Second:4, First:4>>); |
| 88 | encode_map_tbcd([Last], Bin) -> |
| 89 | encode_map_tbcd([], <<Bin/binary, 15:4, Last:4>>). |
| 90 | |
| 91 | encode_map_addr(Bcd) -> |
| 92 | encode_map_tbcd(Bcd). |
| 93 | |
| 94 | |
Harald Welte | 74368cc | 2011-02-09 21:41:36 +0100 | [diff] [blame] | 95 | |
| 96 | parse_addr_string(AddrList) when is_list(AddrList) -> |
| 97 | parse_addr_string(list_to_binary(AddrList)); |
| 98 | parse_addr_string(AddrBin) when is_binary(AddrBin) -> |
| 99 | <<1:1, NatureMap:3, Numplan:4, Remain/binary>> = AddrBin, |
Harald Welte | 4ae92fb | 2011-02-10 17:53:15 +0100 | [diff] [blame] | 100 | PhoneNum = parse_map_addr(Remain), |
Harald Welte | 74368cc | 2011-02-09 21:41:36 +0100 | [diff] [blame] | 101 | NatureIsup = nature_map2isup(NatureMap), |
| 102 | #party_number{nature_of_addr_ind = NatureIsup, |
| 103 | numbering_plan = Numplan, |
| 104 | phone_number = PhoneNum}. |
| 105 | |
| 106 | encode_addr_string(#party_number{nature_of_addr_ind = NatureIsup, |
| 107 | numbering_plan = Numplan, |
| 108 | phone_number = PhoneNum}) -> |
| 109 | NatureMap = nature_isup2map(NatureIsup), |
Harald Welte | 5b14641 | 2011-02-10 17:57:09 +0100 | [diff] [blame] | 110 | PhoneBin = encode_map_addr(PhoneNum), |
Harald Welte | fc281c3 | 2011-02-09 22:39:08 +0100 | [diff] [blame] | 111 | Bin = <<1:1, NatureMap:3, Numplan:4, PhoneBin/binary>>, |
| 112 | binary_to_list(Bin). |
Harald Welte | 268c572 | 2011-02-09 14:38:15 +0100 | [diff] [blame] | 113 | |
| 114 | parse_tcap_msg(MsgBin) when is_binary(MsgBin) -> |
Harald Welte | d08ea8d | 2011-02-09 23:13:48 +0100 | [diff] [blame] | 115 | Msg = binary_to_list(MsgBin), |
| 116 | parse_tcap_msg(Msg); |
| 117 | parse_tcap_msg(Msg) when is_list(Msg) -> |
| 118 | case asn1rt:decode('map', 'MapSpecificPDUs', Msg) of |
Harald Welte | 268c572 | 2011-02-09 14:38:15 +0100 | [diff] [blame] | 119 | {ok, {Type, TcapMsgDec}} -> |
Harald Welte | 21c6b94 | 2011-04-16 20:14:38 +0200 | [diff] [blame^] | 120 | fixup_dialogue({Type, TcapMsgDec}); |
Harald Welte | 268c572 | 2011-02-09 14:38:15 +0100 | [diff] [blame] | 121 | Error -> |
| 122 | Error |
| 123 | end. |
| 124 | |
Harald Welte | 21c6b94 | 2011-04-16 20:14:38 +0200 | [diff] [blame^] | 125 | % Extract the dialoguePortion and feed it through external_1990ify/1 |
| 126 | fixup_dialogue({'begin', Beg = #'MapSpecificPDUs_begin'{dialoguePortion=Dia}}) -> |
| 127 | {'begin', Beg#'MapSpecificPDUs_begin'{dialoguePortion = external_1990ify(Dia)}}; |
| 128 | fixup_dialogue({'end', Beg = #'MapSpecificPDUs_end'{dialoguePortion=Dia}}) -> |
| 129 | {'end', Beg#'MapSpecificPDUs_end'{dialoguePortion = external_1990ify(Dia)}}; |
| 130 | fixup_dialogue({'continue', Beg = #'MapSpecificPDUs_continue'{dialoguePortion=Dia}}) -> |
| 131 | {'continue', Beg#'MapSpecificPDUs_continue'{dialoguePortion = external_1990ify(Dia)}}; |
| 132 | fixup_dialogue({'unidirectional', Beg = #'MapSpecificPDUs_unidirectional'{dialoguePortion=Dia}}) -> |
| 133 | {'unidirectional', Beg#'MapSpecificPDUs_unidirectional'{dialoguePortion = external_1990ify(Dia)}}; |
| 134 | fixup_dialogue(Default) -> |
| 135 | Default. |
| 136 | |
| 137 | % Take the EXTERNAL date type and convert from 1994-style to 1990 with 'single-ASN1-type' |
| 138 | external_1990ify({'EXTERNAL', {syntax, DirRef}, IndirRef, Data}) when is_list(Data); is_binary(Data) -> |
| 139 | #'EXTERNAL'{'direct-reference' = DirRef, |
| 140 | 'indirect-reference' = IndirRef, |
| 141 | encoding = {'single-ASN1-type', Data}}; |
| 142 | external_1990ify(Default) -> |
| 143 | Default. |
| 144 | |
Harald Welte | 0aa140b | 2011-02-09 23:15:22 +0100 | [diff] [blame] | 145 | encode_tcap_msg({Type, TcapMsgDec}) -> |
| 146 | case asn1rt:encode('map', 'MapSpecificPDUs', {Type, TcapMsgDec}) of |
Harald Welte | d08ea8d | 2011-02-09 23:13:48 +0100 | [diff] [blame] | 147 | {ok, List} -> |
| 148 | list_to_binary(List); |
| 149 | Error -> |
| 150 | Error |
| 151 | end. |