blob: cc83d86390918096cdec1f8fe2e35017aab7786e [file] [log] [blame]
Harald Welte1fbcd1c2011-01-15 20:51:53 +01001% ITU-T Q.704 (MTP Level 3) 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/>.
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 Welte1fbcd1c2011-01-15 20:51:53 +010033
34-module(mtp3_codec).
35-author('Harald Welte <laforge@gnumonks.org>').
36-include("mtp3.hrl").
37
Harald Welte1180b7c2012-01-25 01:28:56 +010038-export([parse_mtp3_msg/1, encode_mtp3_msg/1, invert_rout_lbl/1]).
Harald Welte1fbcd1c2011-01-15 20:51:53 +010039
Harald Welte52725272011-04-02 16:48:50 +020040-compile({parse_transform, exprecs}).
41-export_records([mtp3_routing_label, mtp3_msg]).
42
Harald Welte1fbcd1c2011-01-15 20:51:53 +010043% Parse standard routing label according to Section 2.2 of ITU-T Q.704
44parse_mtp3_routing_label(_, LabelBin) when is_binary(LabelBin) ->
Harald Welte09244362012-01-16 21:39:48 +010045 % we need to swap the four bytes and then parse the fields
46 <<Label32:32/little, Remain/binary>> = LabelBin,
47 LabelRev = <<Label32:32/big>>,
48 <<Sls:4/big, Dpc:14/big, Opc:14/big>> = LabelRev,
Harald Welte1fbcd1c2011-01-15 20:51:53 +010049 {ok, #mtp3_routing_label{sig_link_sel = Sls, origin_pc = Opc, dest_pc = Dpc}, Remain}.
50
51parse_mtp3_msg(DataBin) when is_binary(DataBin) ->
52 <<NetInd:2, 0:2, ServiceInd:4, Remain/binary>> = DataBin,
53 {ok, RoutLbl, Payload} = parse_mtp3_routing_label(ServiceInd, Remain),
Harald Welte642a68b2012-01-16 15:59:27 +010054 PayloadDec = decode_payload(ServiceInd, Payload),
Harald Welte1fbcd1c2011-01-15 20:51:53 +010055 #mtp3_msg{network_ind = NetInd, service_ind = ServiceInd, routing_label = RoutLbl,
Harald Welte642a68b2012-01-16 15:59:27 +010056 payload = PayloadDec}.
Harald Welte1fbcd1c2011-01-15 20:51:53 +010057
58
Harald Welteeecba252011-12-08 12:02:02 +010059encode_mtp3_routing_label(#mtp3_routing_label{sig_link_sel = Sls, origin_pc = OpcIn,
60 dest_pc = DpcIn}) ->
61 Opc = osmo_util:pointcode2int(OpcIn),
62 Dpc = osmo_util:pointcode2int(DpcIn),
Harald Welte09244362012-01-16 21:39:48 +010063 % we need to swap the four bytes after encoding the fields
64 <<Label32:32/little>> = <<Sls:4/big, Dpc:14/big, Opc:14/big>>,
65 <<Label32:32/big>>.
Harald Welteeecba252011-12-08 12:02:02 +010066
Harald Welte1fbcd1c2011-01-15 20:51:53 +010067encode_mtp3_msg(#mtp3_msg{network_ind = NetInd, service_ind = ServiceInd,
68 routing_label = RoutLbl, payload = Payload}) ->
69 RoutLblBin = encode_mtp3_routing_label(RoutLbl),
Harald Welte429456f2012-01-16 20:03:37 +010070 PayloadBin = payload_to_binary(ServiceInd, Payload),
Harald Welte642a68b2012-01-16 15:59:27 +010071 <<NetInd:2, 0:2, ServiceInd:4, RoutLblBin/binary, PayloadBin/binary>>.
72
73
Harald Welte429456f2012-01-16 20:03:37 +010074decode_payload(?MTP3_SERV_MTN, Payload) ->
Harald Weltee3b02ed2012-01-16 22:04:44 +010075 <<H1:4, H0:4, Len:4, 0:4, TP/binary>> = Payload,
Harald Welte429456f2012-01-16 20:03:37 +010076 #mtp3mg_msg{h0 = H0, h1 = H1, payload = TP};
77decode_payload(?MTP3_SERV_MGMT, Payload) ->
Harald Weltee3b02ed2012-01-16 22:04:44 +010078 <<H1:4, H0:4, Remain/binary>> = Payload,
Harald Welte429456f2012-01-16 20:03:37 +010079 #mtp3mg_msg{h0 = H0, h1 = H1, payload = Payload};
Harald Welte642a68b2012-01-16 15:59:27 +010080decode_payload(_, Payload) ->
81 Payload.
82
Harald Welte429456f2012-01-16 20:03:37 +010083payload_to_binary(?MTP3_SERV_MTN, #mtp3mg_msg{h0=H0, h1=H1, payload=TP}) ->
Harald Welte642a68b2012-01-16 15:59:27 +010084 Len = byte_size(TP),
Harald Weltee3b02ed2012-01-16 22:04:44 +010085 <<H1:4, H0:4, Len:4, 0:4, TP/binary>>;
Harald Welte429456f2012-01-16 20:03:37 +010086payload_to_binary(?MTP3_SERV_MGMT, #mtp3mg_msg{h0=H0, h1=H1, payload=Payload}) ->
Harald Weltee3b02ed2012-01-16 22:04:44 +010087 <<H1:4, H0:4, Payload/binary>>;
Harald Welte429456f2012-01-16 20:03:37 +010088payload_to_binary(_, Whatever) ->
Harald Welte642a68b2012-01-16 15:59:27 +010089 Whatever.
Harald Welte1fbcd1c2011-01-15 20:51:53 +010090
Harald Welte1180b7c2012-01-25 01:28:56 +010091
92invert_rout_lbl(L = #mtp3_routing_label{origin_pc = Opc, dest_pc = Dpc}) ->
93 L#mtp3_routing_label{origin_pc = Dpc, dest_pc = Opc}.