module IPA_CodecPort {

/* Simple IPA Codec Port, translating between raw TCP octetstring payload
 * towards the IPL4asp port provider, and IPA primitives
 * which carry the decoded IPA data types as payload.
 *
 * (C) 2017-2018 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 IPA_Types all;

	type record IPA_RecvFrom {
		ConnectionId	connId,
		IpaStreamId	streamId,
		IpaExtStreamId	streamIdExt optional,
		octetstring	msg
	}

	type record IPA_Send {
		ConnectionId	connId,
		IpaStreamId	streamId,
		IpaExtStreamId	streamIdExt optional,
		octetstring	msg
	}

	template IPA_Send t_IPA_Send(ConnectionId conn_id, IpaStreamId stream_id,
				octetstring msg, template IpaExtStreamId stream_id_ext := omit) := {
		connId := conn_id,
		streamId := stream_id,
		streamIdExt := stream_id_ext,
		msg := msg
	}

	private function IPL4_to_IPA_RecvFrom(in ASP_RecvFrom pin, out IPA_RecvFrom pout) {
		var PDU_IPA ipa := dec_PDU_IPA(pin.msg);
		pout.connId := pin.connId;
		pout.streamId := ipa.streamId
		pout.streamIdExt := ipa.streamIdExt
		pout.msg := ipa.payloadData;
	} with { extension "prototype(fast)" }

	private function IPA_to_IPL4_Send(in IPA_Send pin, out ASP_Send pout) {
		var PDU_IPA ipa := { 0, pin.streamId, pin.streamIdExt, pin.msg };
		pout.connId := pin.connId;
		pout.proto := { tcp := {} };
		pout.msg := enc_PDU_IPA(ipa);
	} with { extension "prototype(fast)" }

	type port IPA_CODEC_PT message {
		out	IPA_Send;
		in	IPA_RecvFrom,
			ASP_ConnId_ReadyToRelease,
			ASP_Event;
	} with { extension "user IPL4asp_PT
		out(IPA_Send -> ASP_Send:function(IPA_to_IPL4_Send))
		in(ASP_RecvFrom -> IPA_RecvFrom: function(IPL4_to_IPA_RecvFrom);
		   ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple;
		   ASP_Event -> ASP_Event: simple)"
	}
}