blob: df5b6b14de06717e83d22c99097f5869b66ca3da [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) ->
31 <<Sls:4/big, Opc:14/big, Dpc:14/big, Remain/binary>> = LabelBin,
32 {ok, #mtp3_routing_label{sig_link_sel = Sls, origin_pc = Opc, dest_pc = Dpc}, Remain}.
33
34parse_mtp3_msg(DataBin) when is_binary(DataBin) ->
35 <<NetInd:2, 0:2, ServiceInd:4, Remain/binary>> = DataBin,
36 {ok, RoutLbl, Payload} = parse_mtp3_routing_label(ServiceInd, Remain),
37 #mtp3_msg{network_ind = NetInd, service_ind = ServiceInd, routing_label = RoutLbl,
38 payload = Payload}.
39
40
Harald Welteeecba252011-12-08 12:02:02 +010041encode_mtp3_routing_label(#mtp3_routing_label{sig_link_sel = Sls, origin_pc = OpcIn,
42 dest_pc = DpcIn}) ->
43 Opc = osmo_util:pointcode2int(OpcIn),
44 Dpc = osmo_util:pointcode2int(DpcIn),
Harald Welte1fbcd1c2011-01-15 20:51:53 +010045 <<Sls:4/big, Opc:14/big, Dpc:14/big>>.
Harald Welteeecba252011-12-08 12:02:02 +010046
Harald Welte1fbcd1c2011-01-15 20:51:53 +010047encode_mtp3_msg(#mtp3_msg{network_ind = NetInd, service_ind = ServiceInd,
48 routing_label = RoutLbl, payload = Payload}) ->
49 RoutLblBin = encode_mtp3_routing_label(RoutLbl),
50 <<NetInd:2, 0:2, ServiceInd:4, RoutLblBin/binary, Payload/binary>>.
51