module SBC_AP_CodecPort {

/* Simple SBC AP Codec Port, translating between raw SCTP primitives with
 * octetstring payload towards the IPL4asp provider, and SBC-AP primitives
 * which carry the decoded SBC-AP data types as payload.
 *
 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

	import from IPL4asp_PortType all;
	import from IPL4asp_Types all;
	import from SBC_AP_PDU_Descriptions all;
	import from SBC_AP_Types all;

	type record SBC_AP_RecvFrom {
		ConnectionId	connId,
		HostName	remName,
		PortNumber	remPort,
		HostName	locName,
		PortNumber	locPort,
		SBC_AP_PDU	msg
	};

	type record SBC_AP_Send {
		ConnectionId	connId,
		SBC_AP_PDU	msg
	};

	const SctpTuple c_SBC_AP_SctpTuple := {
		sinfo_stream := omit,
		sinfo_ppid := c_SBC_AP_PPID,
		remSocks := omit,
		assocId := omit
	};

	template (value) SBC_AP_Send ts_SBC_AP_Send(ConnectionId connId, template (value) SBC_AP_PDU msg) := {
		connId := connId,
		msg := msg
	}

	template (present) SBC_AP_RecvFrom tr_SBC_AP_Recv(template ConnectionId connId, template SBC_AP_PDU msg) := {
		connId := connId,
		remName := ?,
		remPort := ?,
		locName := ?,
		locPort := ?,
		msg := msg
	}

	private function IPL4_to_SBC_AP_RecvFrom(in ASP_RecvFrom pin, out SBC_AP_RecvFrom pout) {
		pout.connId := pin.connId;
		pout.remName := pin.remName;
		pout.remPort := pin.remPort;
		pout.locName := pin.locName;
		pout.locPort := pin.locPort;
		pout.msg := dec_SBC_AP_PDU(pin.msg);
	} with { extension "prototype(fast)" };

	private function SBC_AP_to_IPL4_Send(in SBC_AP_Send pin, out ASP_Send pout) {
		pout.connId := pin.connId;
		pout.proto := {
			sctp := c_SBC_AP_SctpTuple
		};
		pout.msg := enc_SBC_AP_PDU(pin.msg);
	} with { extension "prototype(fast)" };

	type port SBC_AP_CODEC_PT message {
		out	SBC_AP_Send;
		in	SBC_AP_RecvFrom,
			ASP_ConnId_ReadyToRelease,
			ASP_Event;
	} with { extension "user IPL4asp_PT
		out(SBC_AP_Send -> ASP_Send:function(SBC_AP_to_IPL4_Send))
		in(ASP_RecvFrom -> SBC_AP_RecvFrom: function(IPL4_to_SBC_AP_RecvFrom);
		   ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple;
		   ASP_Event -> ASP_Event: simple)"
	}
}