Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 1 | % M3UA in accordance with RFC4666 (http://tools.ietf.org/html/rfc4666) |
| 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(m3ua_codec). |
| 21 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 22 | -include("m3ua.hrl"). |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 23 | -include("mtp3.hrl"). |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 24 | |
| 25 | -export([parse_m3ua_msg/1, encode_m3ua_msg/1]). |
| 26 | |
| 27 | %-compile(export_all). |
| 28 | |
| 29 | -compile({parse_transform, exprecs}). |
| 30 | -export_records([m3ua_msg]). |
| 31 | |
| 32 | % compute the number of pad bits required after a binary parameter |
| 33 | get_num_pad_bytes(BinLenBytes) -> |
| 34 | case BinLenBytes rem 4 of |
| 35 | 0 -> 0; |
| 36 | Val -> 4 - Val |
| 37 | end. |
| 38 | |
| 39 | parse_m3ua_msg(DataBin) when is_binary(DataBin) -> |
| 40 | <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin, |
| 41 | OptList = parse_m3ua_opts(Remain), |
| 42 | #m3ua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType, |
| 43 | msg_length = MsgLen-4, payload = OptList}; |
| 44 | parse_m3ua_msg(Data) when is_list(Data) -> |
| 45 | parse_m3ua_msg(list_to_binary(Data)). |
| 46 | |
| 47 | parse_m3ua_opts(OptBin) when is_binary(OptBin) -> |
| 48 | parse_m3ua_opts(OptBin, []). |
| 49 | |
Harald Welte | 18dcab0 | 2011-04-03 01:01:58 +0200 | [diff] [blame] | 50 | parse_m3ua_opts(<<>>, OptList) when is_list(OptList) -> |
| 51 | OptList; |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 52 | parse_m3ua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) -> |
| 53 | <<Tag:16/big, Length:16/big, Remain/binary>> = OptBin, |
| 54 | PadLen = get_num_pad_bytes(Length), |
| 55 | LengthNet = Length - 4, |
| 56 | <<CurOpt:LengthNet/binary, 0:PadLen/integer-unit:8, Remain2/binary>> = Remain, |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 57 | NewOpt = parse_m3ua_opt(Tag, CurOpt), |
| 58 | parse_m3ua_opts(Remain2, OptList ++ [NewOpt]). |
| 59 | |
| 60 | parse_m3ua_opt(Opt = ?M3UA_IEI_PROTOCOL_DATA, MsgBin) when is_binary(MsgBin) -> |
| 61 | <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, Mp:8, Sls:8, Payload/binary>> = MsgBin, |
Harald Welte | ce070ef | 2011-04-14 21:52:01 +0200 | [diff] [blame] | 62 | % The idea is to hand back a #mtp3_msg{} to make upper layers beyond |
| 63 | % MTP-TRANSFR.{ind,req} unaware of a MTP3 or M3UA lower layer |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 64 | {Opt, #mtp3_msg{network_ind = Ni, service_ind = Si, |
| 65 | routing_label = #mtp3_routing_label{sig_link_sel = Sls, |
| 66 | origin_pc = Opc, |
| 67 | dest_pc = Dpc}, |
Harald Welte | 9586f95 | 2011-04-03 01:43:38 +0200 | [diff] [blame] | 68 | payload = Payload, m3ua_mp = Mp}}; |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 69 | parse_m3ua_opt(Opt, Msg) -> |
| 70 | {Opt, Msg}. |
| 71 | |
| 72 | |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 73 | |
| 74 | encode_m3ua_msg(#m3ua_msg{version = Version, msg_class = MsgClass, |
| 75 | msg_type = MsgType, payload = OptList}) -> |
| 76 | OptBin = encode_m3ua_opts(OptList), |
Harald Welte | d298d0b | 2011-04-03 01:17:40 +0200 | [diff] [blame] | 77 | MsgLen = byte_size(OptBin) + 8, |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 78 | <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>. |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 79 | |
| 80 | encode_m3ua_opts(OptList) when is_list(OptList) -> |
| 81 | encode_m3ua_opts(OptList, <<>>). |
| 82 | |
| 83 | encode_m3ua_opts([], Bin) -> |
| 84 | Bin; |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 85 | encode_m3ua_opts([{Iei, Attr}|Tail], Bin) -> |
| 86 | OptBin = encode_m3ua_opt(Iei, Attr), |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 87 | encode_m3ua_opts(Tail, <<Bin/binary, OptBin/binary>>). |
| 88 | |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 89 | encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, Mtp3) when is_record(Mtp3, mtp3_msg) -> |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 90 | #mtp3_msg{network_ind = Ni, service_ind = Si, |
| 91 | routing_label = #mtp3_routing_label{sig_link_sel = Sls, |
| 92 | origin_pc = Opc, |
| 93 | dest_pc = Dpc}, |
Harald Welte | 9586f95 | 2011-04-03 01:43:38 +0200 | [diff] [blame] | 94 | payload = Payload, m3ua_mp = Mp} = Mtp3, |
Harald Welte | ce070ef | 2011-04-14 21:52:01 +0200 | [diff] [blame] | 95 | case Mp of |
| 96 | undefined -> MpD = 0; |
| 97 | _ -> MpD = Mp |
| 98 | end, |
| 99 | PayBin = <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, MpD:8, Sls:8, Payload/binary>>, |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 100 | encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, PayBin); |
| 101 | encode_m3ua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) -> |
Harald Welte | d298d0b | 2011-04-03 01:17:40 +0200 | [diff] [blame] | 102 | Length = byte_size(Data) + 4, |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 103 | PadLen = get_num_pad_bytes(Length), |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 104 | <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>. |