Move MGCP_Types.ttcn to library; extract MGCP_Templates.ttcn to library
diff --git a/library/MGCP_Templates.ttcn b/library/MGCP_Templates.ttcn
new file mode 100644
index 0000000..f78bfc7
--- /dev/null
+++ b/library/MGCP_Templates.ttcn
@@ -0,0 +1,188 @@
+module MGCP_Templates {
+
+	import from MGCP_Types all;
+	import from SDP_Types all;
+
+	function f_mgcp_par_append(inout template MgcpParameterList list, template MgcpParameter par) {
+		var integer len := lengthof(list);
+		list[len] := par;
+	}
+
+	/* 3.2.2.6 Connection Mode (sendonly, recvonly, sendrecv, confrnce, inactive, loopback,
+	 * conttest, netwloop, netwtest) */
+	template MgcpParameter t_MgcpParConnMode(template MgcpConnectionMode mode) := { "M", mode };
+
+	/* 3.2.2.2 CallId: maximum 32 hex chars */
+	template MgcpParameter ts_MgcpParCallId(MgcpCallId cid) := {
+		code := "C",
+		val := hex2str(cid)
+	};
+
+	/* 3.2.2.18 RequestIdentifier: Maximum 32 hex chars */
+	template MgcpParameter ts_MgcpParReqId(MgcpRequestId rid) := {
+		code := "X",
+		val := hex2str(rid)
+	};
+
+	/* 3.2.2.10: LocalConnectionOptions (codec, packetization, bandwidth, ToS, eco, gain, silence, ...) */
+	template MgcpParameter t_MgcpParLocConnOpt(template charstring lco) := { "L", lco };
+
+	/* 3.2.2.5: ConnectionId: maximum 32 hex chars */
+	template MgcpParameter ts_MgcpParConnectionId(MgcpConnectionId cid) := {
+		code := "I",
+		val := hex2str(cid)
+	};
+
+	/* osmo-bsc_mgcp implements L/C/M/X only, osmo-mgw adds 'I' */
+	/* SDP: osmo-bsc_mgcp implements Tx of v,o,s,c,t,m,a */
+
+	template MgcpResponse tr_MgcpResp_Err(template MgcpResponseCode code) := {
+		line := {
+			code := code,
+			trans_id := ?,
+			string := ?
+		},
+		params := {},
+		sdp := omit
+	}
+
+	template MgcpCommandLine t_MgcpCmdLine(template charstring verb, template MgcpTransId trans_id, template charstring ep) := {
+		verb := verb,
+		trans_id := trans_id,
+		ep := ep,
+		ver := "1.0"
+	};
+
+	template MgcpCommand ts_CRCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, template SDP_Message sdp := omit) := {
+		line := t_MgcpCmdLine("CRCX", trans_id, ep),
+		params := {
+			t_MgcpParConnMode(mode),
+			ts_MgcpParCallId(call_id),
+			//t_MgcpParReqId(omit),
+			t_MgcpParLocConnOpt("p:20, a:PCMU")
+		},
+		sdp := sdp
+	}
+
+	template MgcpResponse tr_CRCX_ACK := {
+		line := {
+			code := "200",
+			string := "OK"
+		},
+		params:= { { "I", ? }, *},
+		sdp := ?
+	}
+
+	template MgcpCommand ts_MDCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := {
+		line := t_MgcpCmdLine("MDCX", trans_id, ep),
+		params := {
+			t_MgcpParConnMode(mode),
+			ts_MgcpParCallId(call_id),
+			ts_MgcpParConnectionId(conn_id),
+			//t_MgcpParReqId(omit),
+			t_MgcpParLocConnOpt("p:20, a:PCMU")
+		},
+		sdp := sdp
+	}
+
+	/* have a function that generates a template, rather than a template in order to handle
+	 * optional parameters */
+	function ts_DLCX(MgcpTransId trans_id, charstring ep, template MgcpCallId call_id := omit,
+			 template MgcpConnectionId conn_id := omit) return template MgcpCommand {
+		var template MgcpCommand cmd;
+		cmd.line := t_MgcpCmdLine("DLCX", trans_id, ep);
+		cmd.params := {};
+		cmd.sdp := omit;
+		if (isvalue(call_id)) {
+			f_mgcp_par_append(cmd.params, ts_MgcpParCallId(valueof(call_id)));
+			if (isvalue(conn_id)) {
+				f_mgcp_par_append(cmd.params, ts_MgcpParConnectionId(valueof(conn_id)));
+			}
+		}
+		return cmd;
+	}
+
+	/* SDP Templates */
+	template SDP_Origin ts_SDP_origin(charstring addr, charstring session_id,
+					  charstring session_version := "1",
+					  charstring addr_type := "IP4",
+					  charstring user_name := "-") := {
+		user_name := user_name,
+		session_id := session_id,
+		session_version := session_version,
+		net_type := "IN",
+		addr_type := addr_type,
+		addr := addr
+	}
+
+	template SDP_connection ts_SDP_connection_IP(charstring addr, charstring addr_type := "IP4",
+						     template integer ttl := omit,
+						     template integer num_of_addr := omit) :={
+		net_type := "IN",
+		addr_type := addr_type,
+		conn_addr := {
+			addr := addr,
+			ttl := ttl,
+			num_of_addr := num_of_addr
+		}
+	}
+
+	template SDP_time ts_SDP_time(charstring beg, charstring end) := {
+		time_field := {
+			start_time := beg,
+			stop_time := end
+		},
+		time_repeat := omit
+	}
+
+	template SDP_media_desc ts_SDP_media_desc(integer port_number, SDP_fmt_list fmts,
+						  SDP_attribute_list attributes) := {
+		media_field := {
+			media := "audio",
+			ports := {
+				port_number := port_number,
+				num_of_ports := omit
+			},
+			transport := "RTP/AVP",
+			fmts := fmts
+		},
+		information := omit,
+		connections := omit,
+		bandwidth := omit,
+		key := omit,
+		attributes := attributes
+	}
+
+	/* master template for generating SDP based in template arguments */
+	template SDP_Message ts_SDP(charstring local_addr, charstring remote_addr,
+				    charstring session_id, charstring session_version,
+				    integer rtp_port, SDP_fmt_list fmts,
+				    SDP_attribute_list attributes) := {
+		protocol_version := 0,
+		origin := ts_SDP_origin(local_addr, session_id, session_version),
+		session_name := "-",
+		information := omit,
+		uri := omit,
+		emails := omit,
+		phone_numbers := omit,
+		connection := ts_SDP_connection_IP(remote_addr),
+		bandwidth := omit,
+		times := { ts_SDP_time("0","0") },
+		timezone_adjustments := omit,
+		key := omit,
+		attributes := omit,
+		media_list := { ts_SDP_media_desc(rtp_port, fmts, attributes) }
+	}
+
+	template SDP_attribute ts_SDP_rtpmap(integer fmt, charstring val) := {
+		rtpmap := {
+			attr_value := int2str(fmt) & " " & val
+		}
+	}
+	template SDP_attribute ts_SDP_ptime(integer p) := {
+		ptime := {
+			attr_value := int2str(p)
+		}
+	}
+
+}
diff --git a/library/MGCP_Types.ttcn b/library/MGCP_Types.ttcn
new file mode 100644
index 0000000..7d99428
--- /dev/null
+++ b/library/MGCP_Types.ttcn
@@ -0,0 +1,111 @@
+module MGCP_Types {
+	import from SDP_Types all;
+
+	type charstring MgcpVerb ("EPCF", "CRCX", "MDCX", "DLCX", "RQNT", "NTFY",
+				  "AUEP", "AUCX", "RSIP") with {
+		variant "TEXT_CODING(,convert=upper_case,,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 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") with {
+		variant "TEXT_CODING(,convert=upper_case,'([BCINXLMRSDOPEZQTA])|(RC)|(LC)|(ES)|(RM)|(RD)|(PL)|(MD)|(X-Osmo-CP)',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', '(\n)|(\r\n)')"
+		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', '(\n)|(\r\n)')"
+		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', '(\n)|(\r\n)')"
+		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)" };
+
+
+} with { encode "TEXT" }