blob: ec0657c2e5046538d203a6d6c335b1104e505197 [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 Welte9c9553c2011-01-18 18:24:09 +010026% compute the number of pad bits required after a binary parameter
27get_num_pad_bytes(BinLenBytes) ->
28 case BinLenBytes rem 4 of
29 0 -> 0;
30 Val -> 4 - Val
31 end.
32
Harald Welte1f6a0352011-01-15 20:07:45 +010033% parse a binary chunk of options into an options proplist
Harald Welte9c9553c2011-01-18 18:24:09 +010034parse_m2ua_opts(<<>>, OptList) when is_list(OptList) ->
Harald Welte1f6a0352011-01-15 20:07:45 +010035 OptList;
Harald Welte9c9553c2011-01-18 18:24:09 +010036parse_m2ua_opts(OptBin, OptList) when is_list(OptList) ->
Harald Welte1f6a0352011-01-15 20:07:45 +010037 <<Tag:16/big, LengthIncHdr:16/big, Remain/binary>> = OptBin,
38 Length = LengthIncHdr - 4,
Harald Welte9c9553c2011-01-18 18:24:09 +010039 PadLength = get_num_pad_bytes(Length),
40 %io:format("Tag ~w, LenInHdr ~w, Len ~w, PadLen ~w, Remain ~w(~p)~n",
41 % [Tag, LengthIncHdr, Length, PadLength, byte_size(Remain), Remain]),
42 <<Value:Length/binary, PadNextOpts/binary>> = Remain,
43 % this is ridiculous, we cannot use "<<Value:Length/binary,
44 % 0:PadLength, Remain/binary>>" as the last part would not match an
45 % empty binary <<>> anymore. Without the "0:PadLengh" this works
46 % perfectly fine. Now we need some complicated construct and check if
47 % the resulting list would be empty :((
48 if
49 byte_size(PadNextOpts) > PadLength ->
50 <<0:PadLength/integer-unit:8, NextOpts/binary>> = PadNextOpts;
51 true ->
52 NextOpts = <<>>
53 end,
Harald Welte1f6a0352011-01-15 20:07:45 +010054 NewOpt = {Tag, {Length, Value}},
Harald Welte9c9553c2011-01-18 18:24:09 +010055 parse_m2ua_opts(NextOpts, OptList ++ [NewOpt]).
Harald Welte1f6a0352011-01-15 20:07:45 +010056
57% parse a single M2UA message
58parse_m2ua_msgt(_, _, _, Remain) ->
59 parse_m2ua_opts(Remain, []).
60
61% parse a M2UA message binary into a record
62parse_m2ua_msg(DataBin) when is_binary(DataBin) ->
63 <<1:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
64 Parsed = parse_m2ua_msgt(MsgClass, MsgType, MsgLen, Remain),
65 {ok, #m2ua_msg{msg_class = MsgClass, msg_type = MsgType, parameters = Parsed}}.
66
67
68
69% encode a single option
70encode_m2ua_opt({OptNum, {DataBinLen, DataBin}}) when is_integer(OptNum) ->
71 LengthIncHdr = DataBinLen + 4,
Harald Welte9c9553c2011-01-18 18:24:09 +010072 PadLength = get_num_pad_bytes(DataBinLen),
73 case PadLength of
74 0 -> <<OptNum:16/big, LengthIncHdr:16/big, DataBin/binary>>;
75 _ -> <<OptNum:16/big, LengthIncHdr:16/big, DataBin/binary, 0:PadLength/integer-unit:8>>
76 end.
Harald Welte1f6a0352011-01-15 20:07:45 +010077
78% encode a list of options
79encode_m2ua_opts([], OptEnc) ->
Harald Welte9c9553c2011-01-18 18:24:09 +010080 OptEnc;
Harald Welte1f6a0352011-01-15 20:07:45 +010081encode_m2ua_opts([CurOpt|OptPropList], OptEnc) ->
82 CurOptEnc = encode_m2ua_opt(CurOpt),
Harald Welte9c9553c2011-01-18 18:24:09 +010083 encode_m2ua_opts(OptPropList, <<OptEnc/binary, CurOptEnc/binary>>).
Harald Welte1f6a0352011-01-15 20:07:45 +010084
85
86% encode a particular message type
87encode_m2ua_msgt(MsgClass, MsgType, Params) ->
88 OptBin = encode_m2ua_opts(Params, <<>>),
Harald Welte9c9553c2011-01-18 18:24:09 +010089 MsgLenIncHdr = 8 + byte_size(OptBin),
Harald Welte1f6a0352011-01-15 20:07:45 +010090 <<1:8, 0:8, MsgClass:8, MsgType:8, MsgLenIncHdr:32/big, OptBin/binary>>.
91
92% encode a message from record to binary
93encode_m2ua_msg(#m2ua_msg{msg_class = MsgClass, msg_type = MsgType, parameters = Params}) ->
94 encode_m2ua_msgt(MsgClass, MsgType, Params).