blob: 9dc50c23efbaa80412654eaafbef4499c8ce732b [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/>.
19
20-module(mtp3_codec).
21-author('Harald Welte <laforge@gnumonks.org>').
22-include("mtp3.hrl").
23
24-export([parse_mtp3_msg/1, encode_mtp3_msg/1]).
25
Harald Welte52725272011-04-02 16:48:50 +020026-compile({parse_transform, exprecs}).
27-export_records([mtp3_routing_label, mtp3_msg]).
28
Harald Welte1fbcd1c2011-01-15 20:51:53 +010029% Parse standard routing label according to Section 2.2 of ITU-T Q.704
30parse_mtp3_routing_label(_, LabelBin) when is_binary(LabelBin) ->
Harald Welte09244362012-01-16 21:39:48 +010031 % we need to swap the four bytes and then parse the fields
32 <<Label32:32/little, Remain/binary>> = LabelBin,
33 LabelRev = <<Label32:32/big>>,
34 <<Sls:4/big, Dpc:14/big, Opc:14/big>> = LabelRev,
Harald Welte1fbcd1c2011-01-15 20:51:53 +010035 {ok, #mtp3_routing_label{sig_link_sel = Sls, origin_pc = Opc, dest_pc = Dpc}, Remain}.
36
37parse_mtp3_msg(DataBin) when is_binary(DataBin) ->
38 <<NetInd:2, 0:2, ServiceInd:4, Remain/binary>> = DataBin,
39 {ok, RoutLbl, Payload} = parse_mtp3_routing_label(ServiceInd, Remain),
Harald Welte642a68b2012-01-16 15:59:27 +010040 PayloadDec = decode_payload(ServiceInd, Payload),
Harald Welte1fbcd1c2011-01-15 20:51:53 +010041 #mtp3_msg{network_ind = NetInd, service_ind = ServiceInd, routing_label = RoutLbl,
Harald Welte642a68b2012-01-16 15:59:27 +010042 payload = PayloadDec}.
Harald Welte1fbcd1c2011-01-15 20:51:53 +010043
44
Harald Welteeecba252011-12-08 12:02:02 +010045encode_mtp3_routing_label(#mtp3_routing_label{sig_link_sel = Sls, origin_pc = OpcIn,
46 dest_pc = DpcIn}) ->
47 Opc = osmo_util:pointcode2int(OpcIn),
48 Dpc = osmo_util:pointcode2int(DpcIn),
Harald Welte09244362012-01-16 21:39:48 +010049 % we need to swap the four bytes after encoding the fields
50 <<Label32:32/little>> = <<Sls:4/big, Dpc:14/big, Opc:14/big>>,
51 <<Label32:32/big>>.
Harald Welteeecba252011-12-08 12:02:02 +010052
Harald Welte1fbcd1c2011-01-15 20:51:53 +010053encode_mtp3_msg(#mtp3_msg{network_ind = NetInd, service_ind = ServiceInd,
54 routing_label = RoutLbl, payload = Payload}) ->
55 RoutLblBin = encode_mtp3_routing_label(RoutLbl),
Harald Welte429456f2012-01-16 20:03:37 +010056 PayloadBin = payload_to_binary(ServiceInd, Payload),
Harald Welte642a68b2012-01-16 15:59:27 +010057 <<NetInd:2, 0:2, ServiceInd:4, RoutLblBin/binary, PayloadBin/binary>>.
58
59
Harald Welte429456f2012-01-16 20:03:37 +010060decode_payload(?MTP3_SERV_MTN, Payload) ->
Harald Weltee3b02ed2012-01-16 22:04:44 +010061 <<H1:4, H0:4, Len:4, 0:4, TP/binary>> = Payload,
Harald Welte429456f2012-01-16 20:03:37 +010062 #mtp3mg_msg{h0 = H0, h1 = H1, payload = TP};
63decode_payload(?MTP3_SERV_MGMT, Payload) ->
Harald Weltee3b02ed2012-01-16 22:04:44 +010064 <<H1:4, H0:4, Remain/binary>> = Payload,
Harald Welte429456f2012-01-16 20:03:37 +010065 #mtp3mg_msg{h0 = H0, h1 = H1, payload = Payload};
Harald Welte642a68b2012-01-16 15:59:27 +010066decode_payload(_, Payload) ->
67 Payload.
68
Harald Welte429456f2012-01-16 20:03:37 +010069payload_to_binary(?MTP3_SERV_MTN, #mtp3mg_msg{h0=H0, h1=H1, payload=TP}) ->
Harald Welte642a68b2012-01-16 15:59:27 +010070 Len = byte_size(TP),
Harald Weltee3b02ed2012-01-16 22:04:44 +010071 <<H1:4, H0:4, Len:4, 0:4, TP/binary>>;
Harald Welte429456f2012-01-16 20:03:37 +010072payload_to_binary(?MTP3_SERV_MGMT, #mtp3mg_msg{h0=H0, h1=H1, payload=Payload}) ->
Harald Weltee3b02ed2012-01-16 22:04:44 +010073 <<H1:4, H0:4, Payload/binary>>;
Harald Welte429456f2012-01-16 20:03:37 +010074payload_to_binary(_, Whatever) ->
Harald Welte642a68b2012-01-16 15:59:27 +010075 Whatever.
Harald Welte1fbcd1c2011-01-15 20:51:53 +010076