module Iuh_CodecPort {

/* Simple Iuh Codec Port, translating between raw SCTP primitives with
 * octetstring payload towards the IPL4asp provider, and Iuh primitives
 * which carry the decoded Iuh data types as payload.
 *
 * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
 * 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 HNBAP_PDU_Descriptions all;
	import from HNBAP_Types all;
	import from RUA_PDU_Descriptions all;
	import from RUA_Types all;
	import from Iuh_Types all;

	type record Iuh_RecvFrom {
		ConnectionId	connId,
		HostName	remName,
		PortNumber	remPort,
		HostName	locName,
		PortNumber	locPort,
		Iuh_PDU	msg
	};

	template Iuh_RecvFrom t_Iuh_RecvFrom(template Iuh_PDU msg) := {
		connId := ?,
		remName := ?,
		remPort := ?,
		locName := ?,
		locPort := ?,
		msg := msg
	}

	template Iuh_RecvFrom t_Iuh_RecvFrom_HNBAP(template HNBAP_PDU hnbap_msg := ?) := {
		connId := ?,
		remName := ?,
		remPort := ?,
		locName := ?,
		locPort := ?,
		msg := {
			hnbap := hnbap_msg
		}
	}

	template Iuh_RecvFrom t_Iuh_RecvFrom_RUA(template RUA_PDU rua_msg := ?) := {
		connId := ?,
		remName := ?,
		remPort := ?,
		locName := ?,
		locPort := ?,
		msg := {
			rua := rua_msg
		}
	}

	type record Iuh_Send {
		ConnectionId	connId,
		Iuh_PDU	msg
	};

	template Iuh_Send t_Iuh_Send_HNBAP(template ConnectionId connId, template HNBAP_PDU hnbap_msg) := {
		connId := connId,
		msg := {
			hnbap := hnbap_msg
		}
	}

	template Iuh_Send t_Iuh_Send_RUA(template ConnectionId connId, template RUA_PDU rua_msg) := {
		connId := connId,
		msg := {
			rua := rua_msg
		}
	}

	private function IPL4_to_Iuh_RecvFrom(in ASP_RecvFrom pin, out Iuh_RecvFrom pout) {
		pout.connId := pin.connId;
		pout.remName := pin.remName;
		pout.remPort := pin.remPort;
		pout.locName := pin.locName;
		pout.locPort := pin.locPort;
		select (pin.proto.sctp.sinfo_ppid) {
			case (19) {
				pout.msg.rua := dec_RUA_PDU(pin.msg);
			}
			case (20) {
				pout.msg.hnbap := dec_HNBAP_PDU(pin.msg);
			}
			case else {
				pout.msg.payload := pin.msg;
			}
		}
	} with { extension "prototype(fast)" };

	private function Iuh_to_IPL4_Send(in Iuh_Send pin, out ASP_Send pout) {
		var integer sctp_ppid;
		if (ischosen(pin.msg.rua)) {
			sctp_ppid := 19;
			pout.msg := enc_RUA_PDU(pin.msg.rua);
		} else if (ischosen(pin.msg.hnbap)) {
			sctp_ppid := 20;
			pout.msg := enc_HNBAP_PDU(pin.msg.hnbap);
		} else { /*TODO: abort?*/
			sctp_ppid := 0;
			pout.msg := pin.msg.payload;
		}
		pout.connId := pin.connId;
		pout.proto := {
			sctp := {
				sinfo_stream := omit,
				sinfo_ppid := sctp_ppid,
				remSocks := omit,
				assocId := omit
			}
		};
	} with { extension "prototype(fast)" };

	type port Iuh_CODEC_PT message {
		out	Iuh_Send;
		in	Iuh_RecvFrom,
			ASP_ConnId_ReadyToRelease,
			ASP_Event;
	} with { extension "user IPL4asp_PT
		out(Iuh_Send -> ASP_Send:function(Iuh_to_IPL4_Send))
		in(ASP_RecvFrom -> Iuh_RecvFrom: function(IPL4_to_Iuh_RecvFrom);
		   ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple;
		   ASP_Event -> ASP_Event: simple)"
	}
}