asterisk: Implement AMI_Adapter using IPL4 instead of TELNET

Change Telnet_PT to a regular TCP socket for the AMI interface.

I started using Telnet_PT port since initial use of the interface
was done through telnet, but it's not really a telnet interface and
stuff starts becoming difficult to maintain properly when events
(generated by Asterisk at any time) arrive.

The current TEXT decoder/encoder from Titan seems to be struggling in 2
scenarios, so for now we are adding some workarounds in
dec_AMI_Msg_ext() before calling it in order to be able to go forward
and avoid errors:
1- Fields of format "MyFieldName: \r\n" (empty value). I tried changing
the "value" field in record AMI_Field to "optional", but then apparently
the TEXT decoder fails to decode values consisting of several words.
Ideally, I'd expect the TEXT decoder to put an empty "" string in the
"value" field in that case if "optional" is not flagged in the record.
2- Fields of format "MyFieldName: foobar: hey there \r\n" containing a
": " token in the value. I'd expect TEXT decoder to put all subsequent
strings in the last field "value" if no more fields are described in the
record.

Change-Id: Icaf2860c1dd4befa4498f0d176cfadf26cfa8d1d
diff --git a/asterisk/Asterisk_Tests.ttcn b/asterisk/Asterisk_Tests.ttcn
index d2fc0d5..08a5f65 100644
--- a/asterisk/Asterisk_Tests.ttcn
+++ b/asterisk/Asterisk_Tests.ttcn
@@ -39,6 +39,10 @@
 	integer mp_local_ims_port := 5060;
 
 	/* Asterisk AMI: */
+	charstring mp_ami_remote_host := "127.0.0.1";
+	integer mp_ami_remote_port := 5038;
+	charstring mp_ami_local_host := "0.0.0.0";
+	integer mp_ami_local_port := 0;
 	charstring mp_ami_user := "test_user";
 	charstring mp_ami_secret := "1234";
 }
@@ -73,9 +77,17 @@
 /* Initialize connection towards Asterisk AMI */
 private function f_init_ami() runs on test_CT {
 	var charstring id := "Asterisk_Tests_AMI_EMU";
-	vc_AMI := AMI_Adapter_CT.create(id);
+	vc_AMI := AMI_Adapter_CT.create(id) alive;
 	connect(self:AMI_CLIENT, vc_AMI:CLIENT);
-	vc_AMI.start(f_AMI_Adapter_main());
+
+	var AMI_Adapter_Parameters ami_pars := {
+		remote_host := mp_ami_remote_host,
+		remote_port := mp_ami_remote_port,
+		local_host := mp_ami_local_host,
+		local_port := mp_ami_local_port,
+		welcome_str := c_default_AMI_Adapter_pars.welcome_str
+	};
+	vc_AMI.start(f_AMI_Adapter_main(ami_pars));
 
 
 	f_ami_action_login(AMI_CLIENT, mp_ami_user, mp_ami_secret);
@@ -102,6 +114,15 @@
 	log("end of f_init");
 }
 
+function f_shutdown() runs on test_CT {
+	/* Tear down AMI Adapter to avoid it keep receiving data from Asterisk
+	 * and sending it to us after we stopped, causing error (Broken Pipe): */
+	vc_AMI.stop;
+	vc_AMI.done;
+	log("end of ", testcasename());
+	setverdict(pass);
+}
+
 function f_start_handler(void_fn fn, SIPConnHdlrPars pars)
 runs on test_CT return SIPConnHdlr {
 	var SIPConnHdlr vc_conn;
@@ -132,6 +153,7 @@
 	pars := f_init_ConnHdlrPars();
 	vc_conn := f_start_handler(refers(f_TC_internal_registration), pars);
 	vc_conn.done;
+	f_shutdown();
 }
 
 /* Successful SIP MO-MT Call between local clients: */
@@ -210,6 +232,7 @@
 
 	vc_conn[0].done;
 	vc_conn[1].done;
+	f_shutdown();
 }
 
 /* One of the users calls (INVITE) shared extension, which makes all other user
@@ -281,6 +304,7 @@
 	for (var integer i := 0; i < num_conns; i := i + 1) {
 		vc_conn_list[i].done;
 	}
+	f_shutdown();
 }
 testcase TC_internal_call_all_2registered() runs on test_CT {
 	TC_internal_call_all_Nregistered(2);
@@ -293,6 +317,7 @@
 }
 
 testcase TC_selftest() runs on test_CT {
+	f_ami_selftest();
 	f_sip_digest_selftest();
 	setverdict(pass);
 }