blob: 5b29b7e519ad06bf15b999318af08a9f80ec114f [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/>.
Harald Weltef8bf0322012-04-16 13:10:47 +020019%
20% Additional Permission under GNU AGPL version 3 section 7:
21%
22% If you modify this Program, or any covered work, by linking or
23% combining it with runtime libraries of Erlang/OTP as released by
24% Ericsson on http://www.erlang.org (or a modified version of these
25% libraries), containing parts covered by the terms of the Erlang Public
26% License (http://www.erlang.org/EPLICENSE), the licensors of this
27% Program grant you additional permission to convey the resulting work
28% without the need to license the runtime libraries of Erlang/OTP under
29% the GNU Affero General Public License. Corresponding Source for a
30% non-source form of such a combination shall include the source code
31% for the parts of the runtime libraries of Erlang/OTP used as well as
32% that of the covered work.
Harald Welte3bf7cb62011-04-03 00:25:34 +020033
34-module(m3ua_codec).
35-author('Harald Welte <laforge@gnumonks.org>').
36-include("m3ua.hrl").
Harald Weltef2e7cb52011-04-03 01:13:48 +020037-include("mtp3.hrl").
Harald Welte3bf7cb62011-04-03 00:25:34 +020038
39-export([parse_m3ua_msg/1, encode_m3ua_msg/1]).
40
41%-compile(export_all).
42
43-compile({parse_transform, exprecs}).
44-export_records([m3ua_msg]).
45
46% compute the number of pad bits required after a binary parameter
47get_num_pad_bytes(BinLenBytes) ->
48 case BinLenBytes rem 4 of
49 0 -> 0;
50 Val -> 4 - Val
51 end.
52
53parse_m3ua_msg(DataBin) when is_binary(DataBin) ->
54 <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
55 OptList = parse_m3ua_opts(Remain),
56 #m3ua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType,
57 msg_length = MsgLen-4, payload = OptList};
58parse_m3ua_msg(Data) when is_list(Data) ->
59 parse_m3ua_msg(list_to_binary(Data)).
60
61parse_m3ua_opts(OptBin) when is_binary(OptBin) ->
62 parse_m3ua_opts(OptBin, []).
63
Harald Welte18dcab02011-04-03 01:01:58 +020064parse_m3ua_opts(<<>>, OptList) when is_list(OptList) ->
65 OptList;
Harald Welte3bf7cb62011-04-03 00:25:34 +020066parse_m3ua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) ->
67 <<Tag:16/big, Length:16/big, Remain/binary>> = OptBin,
68 PadLen = get_num_pad_bytes(Length),
69 LengthNet = Length - 4,
70 <<CurOpt:LengthNet/binary, 0:PadLen/integer-unit:8, Remain2/binary>> = Remain,
Harald Weltef2e7cb52011-04-03 01:13:48 +020071 NewOpt = parse_m3ua_opt(Tag, CurOpt),
72 parse_m3ua_opts(Remain2, OptList ++ [NewOpt]).
73
74parse_m3ua_opt(Opt = ?M3UA_IEI_PROTOCOL_DATA, MsgBin) when is_binary(MsgBin) ->
75 <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, Mp:8, Sls:8, Payload/binary>> = MsgBin,
Harald Weltece070ef2011-04-14 21:52:01 +020076 % The idea is to hand back a #mtp3_msg{} to make upper layers beyond
77 % MTP-TRANSFR.{ind,req} unaware of a MTP3 or M3UA lower layer
Harald Weltef2e7cb52011-04-03 01:13:48 +020078 {Opt, #mtp3_msg{network_ind = Ni, service_ind = Si,
79 routing_label = #mtp3_routing_label{sig_link_sel = Sls,
80 origin_pc = Opc,
81 dest_pc = Dpc},
Harald Welte9586f952011-04-03 01:43:38 +020082 payload = Payload, m3ua_mp = Mp}};
Harald Weltef2e7cb52011-04-03 01:13:48 +020083parse_m3ua_opt(Opt, Msg) ->
84 {Opt, Msg}.
85
86
Harald Welte3bf7cb62011-04-03 00:25:34 +020087
88encode_m3ua_msg(#m3ua_msg{version = Version, msg_class = MsgClass,
89 msg_type = MsgType, payload = OptList}) ->
90 OptBin = encode_m3ua_opts(OptList),
Harald Welted298d0b2011-04-03 01:17:40 +020091 MsgLen = byte_size(OptBin) + 8,
Harald Welte0dc999f2011-04-03 01:36:37 +020092 <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
Harald Welte3bf7cb62011-04-03 00:25:34 +020093
94encode_m3ua_opts(OptList) when is_list(OptList) ->
95 encode_m3ua_opts(OptList, <<>>).
96
97encode_m3ua_opts([], Bin) ->
98 Bin;
Harald Welte0dc999f2011-04-03 01:36:37 +020099encode_m3ua_opts([{Iei, Attr}|Tail], Bin) ->
100 OptBin = encode_m3ua_opt(Iei, Attr),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200101 encode_m3ua_opts(Tail, <<Bin/binary, OptBin/binary>>).
102
Harald Welte0dc999f2011-04-03 01:36:37 +0200103encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, Mtp3) when is_record(Mtp3, mtp3_msg) ->
Harald Weltef2e7cb52011-04-03 01:13:48 +0200104 #mtp3_msg{network_ind = Ni, service_ind = Si,
105 routing_label = #mtp3_routing_label{sig_link_sel = Sls,
Harald Welteeecba252011-12-08 12:02:02 +0100106 origin_pc = OpcIn,
107 dest_pc = DpcIn},
Harald Welte9586f952011-04-03 01:43:38 +0200108 payload = Payload, m3ua_mp = Mp} = Mtp3,
Harald Welteeecba252011-12-08 12:02:02 +0100109 Opc = osmo_util:pointcode2int(OpcIn),
110 Dpc = osmo_util:pointcode2int(DpcIn),
Harald Weltece070ef2011-04-14 21:52:01 +0200111 case Mp of
112 undefined -> MpD = 0;
113 _ -> MpD = Mp
114 end,
115 PayBin = <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, MpD:8, Sls:8, Payload/binary>>,
Harald Welte0dc999f2011-04-03 01:36:37 +0200116 encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, PayBin);
117encode_m3ua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) ->
Harald Welted298d0b2011-04-03 01:17:40 +0200118 Length = byte_size(Data) + 4,
Harald Welte3bf7cb62011-04-03 00:25:34 +0200119 PadLen = get_num_pad_bytes(Length),
Harald Welte0dc999f2011-04-03 01:36:37 +0200120 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>.