blob: 03ec923743bb15ba517076f5a761a355497338d6 [file] [log] [blame]
Harald Welte3bf7cb62011-04-03 00:25:34 +02001% 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 Weltef2e7cb52011-04-03 01:13:48 +020023-include("mtp3.hrl").
Harald Welte3bf7cb62011-04-03 00:25:34 +020024
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
33get_num_pad_bytes(BinLenBytes) ->
34 case BinLenBytes rem 4 of
35 0 -> 0;
36 Val -> 4 - Val
37 end.
38
39parse_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};
44parse_m3ua_msg(Data) when is_list(Data) ->
45 parse_m3ua_msg(list_to_binary(Data)).
46
47parse_m3ua_opts(OptBin) when is_binary(OptBin) ->
48 parse_m3ua_opts(OptBin, []).
49
Harald Welte18dcab02011-04-03 01:01:58 +020050parse_m3ua_opts(<<>>, OptList) when is_list(OptList) ->
51 OptList;
Harald Welte3bf7cb62011-04-03 00:25:34 +020052parse_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 Weltef2e7cb52011-04-03 01:13:48 +020057 NewOpt = parse_m3ua_opt(Tag, CurOpt),
58 parse_m3ua_opts(Remain2, OptList ++ [NewOpt]).
59
60parse_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,
62 {Opt, #mtp3_msg{network_ind = Ni, service_ind = Si,
63 routing_label = #mtp3_routing_label{sig_link_sel = Sls,
64 origin_pc = Opc,
65 dest_pc = Dpc},
66 payload = Payload}};
67parse_m3ua_opt(Opt, Msg) ->
68 {Opt, Msg}.
69
70
Harald Welte3bf7cb62011-04-03 00:25:34 +020071
72encode_m3ua_msg(#m3ua_msg{version = Version, msg_class = MsgClass,
73 msg_type = MsgType, payload = OptList}) ->
74 OptBin = encode_m3ua_opts(OptList),
Harald Welted298d0b2011-04-03 01:17:40 +020075 MsgLen = byte_size(OptBin) + 8,
Harald Welte0dc999f2011-04-03 01:36:37 +020076 <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
Harald Welte3bf7cb62011-04-03 00:25:34 +020077
78encode_m3ua_opts(OptList) when is_list(OptList) ->
79 encode_m3ua_opts(OptList, <<>>).
80
81encode_m3ua_opts([], Bin) ->
82 Bin;
Harald Welte0dc999f2011-04-03 01:36:37 +020083encode_m3ua_opts([{Iei, Attr}|Tail], Bin) ->
84 OptBin = encode_m3ua_opt(Iei, Attr),
Harald Welte3bf7cb62011-04-03 00:25:34 +020085 encode_m3ua_opts(Tail, <<Bin/binary, OptBin/binary>>).
86
Harald Welte0dc999f2011-04-03 01:36:37 +020087encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, Mtp3) when is_record(Mtp3, mtp3_msg) ->
Harald Weltef2e7cb52011-04-03 01:13:48 +020088 #mtp3_msg{network_ind = Ni, service_ind = Si,
89 routing_label = #mtp3_routing_label{sig_link_sel = Sls,
90 origin_pc = Opc,
91 dest_pc = Dpc},
92 payload = Payload} = Mtp3,
Harald Welte0dc999f2011-04-03 01:36:37 +020093 PayBin = <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, 0:8, Sls:8, Payload/binary>>,
94 encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, PayBin);
95encode_m3ua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) ->
Harald Welted298d0b2011-04-03 01:17:40 +020096 Length = byte_size(Data) + 4,
Harald Welte3bf7cb62011-04-03 00:25:34 +020097 PadLen = get_num_pad_bytes(Length),
Harald Welte0dc999f2011-04-03 01:36:37 +020098 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>.