blob: a1b03f8f4aefcc69778d286d03ff09b5f7e57063 [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,
Harald Weltece070ef2011-04-14 21:52:01 +020062 % 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 Weltef2e7cb52011-04-03 01:13:48 +020064 {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 Welte9586f952011-04-03 01:43:38 +020068 payload = Payload, m3ua_mp = Mp}};
Harald Weltef2e7cb52011-04-03 01:13:48 +020069parse_m3ua_opt(Opt, Msg) ->
70 {Opt, Msg}.
71
72
Harald Welte3bf7cb62011-04-03 00:25:34 +020073
74encode_m3ua_msg(#m3ua_msg{version = Version, msg_class = MsgClass,
75 msg_type = MsgType, payload = OptList}) ->
76 OptBin = encode_m3ua_opts(OptList),
Harald Welted298d0b2011-04-03 01:17:40 +020077 MsgLen = byte_size(OptBin) + 8,
Harald Welte0dc999f2011-04-03 01:36:37 +020078 <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
Harald Welte3bf7cb62011-04-03 00:25:34 +020079
80encode_m3ua_opts(OptList) when is_list(OptList) ->
81 encode_m3ua_opts(OptList, <<>>).
82
83encode_m3ua_opts([], Bin) ->
84 Bin;
Harald Welte0dc999f2011-04-03 01:36:37 +020085encode_m3ua_opts([{Iei, Attr}|Tail], Bin) ->
86 OptBin = encode_m3ua_opt(Iei, Attr),
Harald Welte3bf7cb62011-04-03 00:25:34 +020087 encode_m3ua_opts(Tail, <<Bin/binary, OptBin/binary>>).
88
Harald Welte0dc999f2011-04-03 01:36:37 +020089encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, Mtp3) when is_record(Mtp3, mtp3_msg) ->
Harald Weltef2e7cb52011-04-03 01:13:48 +020090 #mtp3_msg{network_ind = Ni, service_ind = Si,
91 routing_label = #mtp3_routing_label{sig_link_sel = Sls,
Harald Welteeecba252011-12-08 12:02:02 +010092 origin_pc = OpcIn,
93 dest_pc = DpcIn},
Harald Welte9586f952011-04-03 01:43:38 +020094 payload = Payload, m3ua_mp = Mp} = Mtp3,
Harald Welteeecba252011-12-08 12:02:02 +010095 Opc = osmo_util:pointcode2int(OpcIn),
96 Dpc = osmo_util:pointcode2int(DpcIn),
Harald Weltece070ef2011-04-14 21:52:01 +020097 case Mp of
98 undefined -> MpD = 0;
99 _ -> MpD = Mp
100 end,
101 PayBin = <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, MpD:8, Sls:8, Payload/binary>>,
Harald Welte0dc999f2011-04-03 01:36:37 +0200102 encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, PayBin);
103encode_m3ua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) ->
Harald Welted298d0b2011-04-03 01:17:40 +0200104 Length = byte_size(Data) + 4,
Harald Welte3bf7cb62011-04-03 00:25:34 +0200105 PadLen = get_num_pad_bytes(Length),
Harald Welte0dc999f2011-04-03 01:36:37 +0200106 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>.