module MGCP_Types {

/* Definition of abstract types for the MGCP protocol as specified in
 * IETF RFC 3435.  Uses the TITAN "TEXT" codec to auto-generate encoder/decoder
 * functions, as well as the SDP type definitions and coder from Ericsson.
 *
 * (C) 2017 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.
 */

	import from SDP_Types all;

	type charstring MgcpVerb ("EPCF", "CRCX", "MDCX", "DLCX", "RQNT", "NTFY",
				  "AUEP", "AUCX", "RSIP") with {
		/* see https://www.eclipse.org/forums/index.php/t/1088893/ on why this
		 * match expression is needed here */
		variant "TEXT_CODING(,convert=upper_case,'((EPCF)|(CRCX)|(MDCX)|(DLCX)|(RQNT)|(NTFY)|(AUEP)|(AUCX)|(RSIP))',case_insensitive)"
	};
	type charstring MgcpTransId 	(pattern "\d#(1,9)");
	type charstring MgcpEndpoint	(pattern "*@*");
	type hexstring MgcpCallId	length(1..32);	/* 3.2.2.2 */
	type hexstring MgcpConnectionId	length(1..32);	/* 3.2.2.5 */
	type hexstring MgcpRequestId	length(1..32);	/* 3.2.2.18 */
	type integer MgcpOsmuxCID	(-1 .. 255);
	type charstring MgcpResponseCode (pattern "\d#(3)");

	type charstring MgcpInfoCode ("B", "C", "I", "N", "X", "L", "M", "R",
				      "S", "D", "O", "P", "E", "Z", "Q", "T",
				      "RC", "LC", "A", "ES", "RM", "RD", "PL",
				      "MD", "X-OSMO-CP", "X-OSMO-IGN", "X-OSMUX") with {
		variant "TEXT_CODING(,convert=upper_case,'([BCINXLMRSDOPEZQTA])|(RC)|(LC)|(ES)|(RM)|(RD)|(PL)|(MD)|(X-OSMO-CP)|(X-OSMO-IGN)|(X-OSMUX)',case_insensitive)"
	};

	/* 3.2.2.6 */
	type charstring MgcpConnectionMode ("sendonly", "recvonly", "sendrecv", "confrnce",
					    "inactive", "loopback", "conttest", "netwloop",
					    "netwtest");

	/* 3.2.2.10 */
	type charstring MgcpLocalConnOptKeys ("a", "p", "b", "t", "e", "gc", "s", "r", "k",
					      "nt", "r");

	type charstring MgcpVersion (pattern "\d.\d") with {
		variant "BEGIN('MGCP ')"
	}

	type record MgcpCommandLine {
		MgcpVerb	verb,
		MgcpTransId	trans_id,
		MgcpEndpoint	ep,
		MgcpVersion	ver
	} with {
		variant "SEPARATOR(' ', '[\t ]+')"
		variant "END('\r\n', '([\r\n])|(\r\n)')"
	}

	type record MgcpParameter {
		MgcpInfoCode	code,
		charstring	val optional
	} with {
		variant "BEGIN('')"
		variant "SEPARATOR(': ', ':[\t ]+')"
		variant "END('\r\n', '([\r\n])|(\r\n)')"
	}

	type set of MgcpParameter MgcpParameterList with {
		variant "BEGIN('')"
	};

	type record MgcpCommand {
		MgcpCommandLine		line,
		MgcpParameterList 	params optional,
		SDP_Message		sdp optional
	} with {
		variant "BEGIN('')"
		variant (sdp) "BEGIN('\r\n','([\r\n])|(\r\n)')"
	}

	external function enc_MgcpCommand(in MgcpCommand id) return charstring
		with { extension "prototype(convert) encode(TEXT)" };
	external function dec_MgcpCommand(in charstring  id) return MgcpCommand
		with { extension "prototype(convert) decode(TEXT)" };

	type record MgcpResponseLine {
		MgcpResponseCode	code,
		MgcpTransId		trans_id,
		charstring		string optional
	} with {
		variant "SEPARATOR(' ', '[\t ]+')"
		variant "END('\r\n', '([\r\n])|(\r\n)')"
	}

	type record MgcpResponse {
		MgcpResponseLine	line,
		MgcpParameterList	params optional,
		SDP_Message		sdp optional
	} with {
		variant "BEGIN('')"
		variant (sdp) "BEGIN('\r\n','([\r\n])|(\r\n)')"
	}

	external function enc_MgcpResponse(in MgcpResponse id) return charstring
		with { extension "prototype(convert) encode(TEXT)" };
	external function dec_MgcpResponse(in charstring  id) return MgcpResponse
		with { extension "prototype(convert) decode(TEXT)" };

	type union MgcpMessage {
		MgcpCommand	command,
		MgcpResponse	response
	} with {
		variant "BEGIN('')"
	}

	external function enc_MgcpMessage(in MgcpMessage id) return charstring
		with { extension "prototype(convert) encode(TEXT)" };
	external function dec_MgcpMessage(in charstring  id) return MgcpMessage
		with { extension "prototype(convert) decode(TEXT)" };

	/* IANA / 3gpp assigned payload type numbers */
	type enumerated SDP_FIELD_PayloadType {
		PT_PCMU(0),
		PT_GSM(3),
		PT_PCMA(8),
		PT_G729(18),
		PT_GSMEFR(110),
		PT_GSMHR(111),
		PT_AMR(112),
		PT_AMRWB(113),
		PT_CSD(120)
	}

} with { encode "TEXT" }