Harald Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 1 | % 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 Welte | 5272527 | 2011-04-02 16:48:50 +0200 | [diff] [blame] | 26 | -compile({parse_transform, exprecs}). |
| 27 | -export_records([mtp3_routing_label, mtp3_msg]). |
| 28 | |
Harald Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 29 | % Parse standard routing label according to Section 2.2 of ITU-T Q.704 |
| 30 | parse_mtp3_routing_label(_, LabelBin) when is_binary(LabelBin) -> |
Harald Welte | 0924436 | 2012-01-16 21:39:48 +0100 | [diff] [blame^] | 31 | % 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 Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 35 | {ok, #mtp3_routing_label{sig_link_sel = Sls, origin_pc = Opc, dest_pc = Dpc}, Remain}. |
| 36 | |
| 37 | parse_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 Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 40 | PayloadDec = decode_payload(ServiceInd, Payload), |
Harald Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 41 | #mtp3_msg{network_ind = NetInd, service_ind = ServiceInd, routing_label = RoutLbl, |
Harald Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 42 | payload = PayloadDec}. |
Harald Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 43 | |
| 44 | |
Harald Welte | eecba25 | 2011-12-08 12:02:02 +0100 | [diff] [blame] | 45 | encode_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 Welte | 0924436 | 2012-01-16 21:39:48 +0100 | [diff] [blame^] | 49 | % 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 Welte | eecba25 | 2011-12-08 12:02:02 +0100 | [diff] [blame] | 52 | |
Harald Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 53 | encode_mtp3_msg(#mtp3_msg{network_ind = NetInd, service_ind = ServiceInd, |
| 54 | routing_label = RoutLbl, payload = Payload}) -> |
| 55 | RoutLblBin = encode_mtp3_routing_label(RoutLbl), |
Harald Welte | 429456f | 2012-01-16 20:03:37 +0100 | [diff] [blame] | 56 | PayloadBin = payload_to_binary(ServiceInd, Payload), |
Harald Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 57 | <<NetInd:2, 0:2, ServiceInd:4, RoutLblBin/binary, PayloadBin/binary>>. |
| 58 | |
| 59 | |
Harald Welte | 429456f | 2012-01-16 20:03:37 +0100 | [diff] [blame] | 60 | decode_payload(?MTP3_SERV_MTN, Payload) -> |
Harald Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 61 | <<H0:4, H1:4, _:4, Len:4, TP/binary>> = Payload, |
Harald Welte | 429456f | 2012-01-16 20:03:37 +0100 | [diff] [blame] | 62 | #mtp3mg_msg{h0 = H0, h1 = H1, payload = TP}; |
| 63 | decode_payload(?MTP3_SERV_MGMT, Payload) -> |
| 64 | <<H0:4, H1:4, Remain/binary>> = Payload, |
| 65 | #mtp3mg_msg{h0 = H0, h1 = H1, payload = Payload}; |
Harald Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 66 | decode_payload(_, Payload) -> |
| 67 | Payload. |
| 68 | |
Harald Welte | 429456f | 2012-01-16 20:03:37 +0100 | [diff] [blame] | 69 | payload_to_binary(?MTP3_SERV_MTN, #mtp3mg_msg{h0=H0, h1=H1, payload=TP}) -> |
Harald Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 70 | Len = byte_size(TP), |
| 71 | <<H0:4, H1:4, 0:4, Len:4, TP/binary>>; |
Harald Welte | 429456f | 2012-01-16 20:03:37 +0100 | [diff] [blame] | 72 | payload_to_binary(?MTP3_SERV_MGMT, #mtp3mg_msg{h0=H0, h1=H1, payload=Payload}) -> |
| 73 | <<H0:4, H1:4, Payload/binary>>; |
| 74 | payload_to_binary(_, Whatever) -> |
Harald Welte | 642a68b | 2012-01-16 15:59:27 +0100 | [diff] [blame] | 75 | Whatever. |
Harald Welte | 1fbcd1c | 2011-01-15 20:51:53 +0100 | [diff] [blame] | 76 | |