blob: 32bec002f21d3e7fab0b871e6121255f9ae7117e [file] [log] [blame]
Harald Welte1f6a0352011-01-15 20:07:45 +01001% RFC 3331 MTP2 User Adaption Layer coding / decoding
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(m2ua_codec).
21-author('Harald Welte <laforge@gnumonks.org>').
22-include("m2ua.hrl").
23
24-export([parse_m2ua_msg/1, encode_m2ua_msg/1]).
25
Harald Welte52725272011-04-02 16:48:50 +020026-compile({parse_transform, exprecs}).
27-export_records([m2ua_msg]).
28
Harald Welte9c9553c2011-01-18 18:24:09 +010029% compute the number of pad bits required after a binary parameter
30get_num_pad_bytes(BinLenBytes) ->
31 case BinLenBytes rem 4 of
32 0 -> 0;
33 Val -> 4 - Val
34 end.
35
Harald Welte1f6a0352011-01-15 20:07:45 +010036% parse a binary chunk of options into an options proplist
Harald Welte9c9553c2011-01-18 18:24:09 +010037parse_m2ua_opts(<<>>, OptList) when is_list(OptList) ->
Harald Welte1f6a0352011-01-15 20:07:45 +010038 OptList;
Harald Welte9c9553c2011-01-18 18:24:09 +010039parse_m2ua_opts(OptBin, OptList) when is_list(OptList) ->
Harald Welte1f6a0352011-01-15 20:07:45 +010040 <<Tag:16/big, LengthIncHdr:16/big, Remain/binary>> = OptBin,
41 Length = LengthIncHdr - 4,
Harald Welte9c9553c2011-01-18 18:24:09 +010042 PadLength = get_num_pad_bytes(Length),
43 %io:format("Tag ~w, LenInHdr ~w, Len ~w, PadLen ~w, Remain ~w(~p)~n",
44 % [Tag, LengthIncHdr, Length, PadLength, byte_size(Remain), Remain]),
45 <<Value:Length/binary, PadNextOpts/binary>> = Remain,
46 % this is ridiculous, we cannot use "<<Value:Length/binary,
47 % 0:PadLength, Remain/binary>>" as the last part would not match an
48 % empty binary <<>> anymore. Without the "0:PadLengh" this works
49 % perfectly fine. Now we need some complicated construct and check if
50 % the resulting list would be empty :((
51 if
52 byte_size(PadNextOpts) > PadLength ->
53 <<0:PadLength/integer-unit:8, NextOpts/binary>> = PadNextOpts;
54 true ->
55 NextOpts = <<>>
56 end,
Harald Welte1f6a0352011-01-15 20:07:45 +010057 NewOpt = {Tag, {Length, Value}},
Harald Welte9c9553c2011-01-18 18:24:09 +010058 parse_m2ua_opts(NextOpts, OptList ++ [NewOpt]).
Harald Welte1f6a0352011-01-15 20:07:45 +010059
60% parse a single M2UA message
61parse_m2ua_msgt(_, _, _, Remain) ->
62 parse_m2ua_opts(Remain, []).
63
64% parse a M2UA message binary into a record
65parse_m2ua_msg(DataBin) when is_binary(DataBin) ->
66 <<1:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
67 Parsed = parse_m2ua_msgt(MsgClass, MsgType, MsgLen, Remain),
68 {ok, #m2ua_msg{msg_class = MsgClass, msg_type = MsgType, parameters = Parsed}}.
69
70
71
72% encode a single option
73encode_m2ua_opt({OptNum, {DataBinLen, DataBin}}) when is_integer(OptNum) ->
74 LengthIncHdr = DataBinLen + 4,
Harald Welte9c9553c2011-01-18 18:24:09 +010075 PadLength = get_num_pad_bytes(DataBinLen),
76 case PadLength of
77 0 -> <<OptNum:16/big, LengthIncHdr:16/big, DataBin/binary>>;
78 _ -> <<OptNum:16/big, LengthIncHdr:16/big, DataBin/binary, 0:PadLength/integer-unit:8>>
79 end.
Harald Welte1f6a0352011-01-15 20:07:45 +010080
81% encode a list of options
82encode_m2ua_opts([], OptEnc) ->
Harald Welte9c9553c2011-01-18 18:24:09 +010083 OptEnc;
Harald Welte1f6a0352011-01-15 20:07:45 +010084encode_m2ua_opts([CurOpt|OptPropList], OptEnc) ->
85 CurOptEnc = encode_m2ua_opt(CurOpt),
Harald Welte9c9553c2011-01-18 18:24:09 +010086 encode_m2ua_opts(OptPropList, <<OptEnc/binary, CurOptEnc/binary>>).
Harald Welte1f6a0352011-01-15 20:07:45 +010087
88
89% encode a particular message type
90encode_m2ua_msgt(MsgClass, MsgType, Params) ->
91 OptBin = encode_m2ua_opts(Params, <<>>),
Harald Welte9c9553c2011-01-18 18:24:09 +010092 MsgLenIncHdr = 8 + byte_size(OptBin),
Harald Welte1f6a0352011-01-15 20:07:45 +010093 <<1:8, 0:8, MsgClass:8, MsgType:8, MsgLenIncHdr:32/big, OptBin/binary>>.
94
95% encode a message from record to binary
96encode_m2ua_msg(#m2ua_msg{msg_class = MsgClass, msg_type = MsgType, parameters = Params}) ->
97 encode_m2ua_msgt(MsgClass, MsgType, Params).