pespin has submitted this change. ( 
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/36610?usp=email )

Change subject: asterisk: Use Titan TEXT encoder to implement AMI record enc/dec
......................................................................

asterisk: Use Titan TEXT encoder to implement AMI record enc/dec

f_ami_wait_for_prompt_str had to be fixed since it was returning content
like this:
"Response: Success\nMessage: Authentication accepted"
Instead, the TEXT encoding expects:
"Response: Success\nMessage: Authentication accepted\n"
which is actually more in line with proper expectancies.

Change-Id: If179e45307a7e42b81b28db6ee4b7bf5eeb8b5f1
---
M asterisk/AMI_Functions.ttcn
1 file changed, 34 insertions(+), 48 deletions(-)

Approvals:
  fixeria: Looks good to me, but someone else must approve
  osmith: Looks good to me, approved
  Jenkins Builder: Verified




diff --git a/asterisk/AMI_Functions.ttcn b/asterisk/AMI_Functions.ttcn
index 53a7964..d5139f5 100644
--- a/asterisk/AMI_Functions.ttcn
+++ b/asterisk/AMI_Functions.ttcn
@@ -32,8 +32,21 @@
 type record AMI_Field {
        charstring      key,
        charstring      val
+} with {
+       encode "TEXT"
+       variant "SEPARATOR(': ', ':\s+')"
 };
-type set of AMI_Field AMI_Msg;
+
+type set of AMI_Field AMI_Msg with {
+       encode "TEXT"
+       variant "SEPARATOR('\r\n', '(\r\n)|[\n]')"
+       variant "END('\r\n', '(\r\n)|[\n]')"
+};
+
+external function enc_AMI_Msg(in AMI_Msg msg) return charstring
+       with { extension "prototype(convert) encode(TEXT)" }
+external function dec_AMI_Msg(in charstring stream) return AMI_Msg
+       with { extension "prototype(convert) decode(TEXT)" }

 template (value) AMI_Field
 ts_AMI_Field(template (value) charstring key,
@@ -102,49 +115,6 @@
  * Functions:
  */

-function f_AMI_Field_from_str(charstring str) return AMI_Field {
-       var AMI_Field field;
-       /* "each field is a key value pair delineated by a ':'.
-        * A single space MUST follow the ':' and precede the value. "*/
-       var integer pos := f_strstr(str, ": ", 0);
-       if (pos < 0) {
-               Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
-                                       log2str("Failed parsing AMI_Field: ", 
str));
-       }
-       field.key := substr(str, 0, pos);
-       /* skip ": " */
-       pos := pos + 2;
-       field.val := substr(str, pos, lengthof(str) - pos);
-       return field;
-}
-
-function f_AMI_Msg_from_str(charstring str) return AMI_Msg {
-       var AMI_Msg msg := {};
-       var Misc_Helpers.ro_charstring lines := f_str_split(str, "\n");
-
-       for (var integer i := 0; i < lengthof(lines); i := i + 1) {
-               var charstring line := lines[i];
-               var AMI_Field field := f_AMI_Field_from_str(lines[i]);
-               msg := msg & { field };
-       }
-       return msg;
-}
-
-function f_AMI_Field_to_str(AMI_Field field) return charstring {
-       return field.key & ": " & field.val;
-}
-
-function f_AMI_Msg_to_str(AMI_Msg msg) return charstring {
-       var charstring str := "";
-
-       for (var integer i := 0; i < lengthof(msg); i := i + 1) {
-               str := str & f_AMI_Field_to_str(msg[i]) & "\r\n";
-       }
-
-       str := str & "\r\n";
-       return str;
-}
-
 private function f_ami_wait_for_prompt_str(TELNETasp_PT pt, charstring 
log_label := "(?)")
 return charstring {
        var charstring rx, buf := "";
@@ -153,7 +123,7 @@

        T.start(mp_ami_prompt_timeout);
        alt {
-       [] pt.receive(pattern "\n") { };
+       [] pt.receive(pattern "\n") { buf := buf & "\n" };
        [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
        [] pt.receive(integer:?) -> value fd {
                if (fd == -1) {
@@ -174,7 +144,7 @@

 function f_ami_wait_for_prompt(TELNETasp_PT pt, charstring log_label := "(?)") 
return AMI_Msg {
        var charstring buf := f_ami_wait_for_prompt_str(pt, log_label);
-       var AMI_Msg msg := f_AMI_Msg_from_str(buf);
+       var AMI_Msg msg := dec_AMI_Msg(buf);
        return msg;
 }

@@ -185,9 +155,9 @@
 }

 function f_ami_transceive_ret(TELNETasp_PT pt, template (value) AMI_Msg 
tx_msg) return AMI_Msg {
-       var charstring tx_txt := f_AMI_Msg_to_str(valueof(tx_msg));
+       var charstring tx_txt := enc_AMI_Msg(valueof(tx_msg));
        var charstring resp_txt := f_ami_transceive_ret_str(pt, tx_txt);
-       return f_AMI_Msg_from_str(resp_txt);
+       return dec_AMI_Msg(resp_txt);
 }

 function f_ami_transceive_match(TELNETasp_PT pt,

--
To view, visit https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/36610?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ttcn3-hacks
Gerrit-Branch: master
Gerrit-Change-Id: If179e45307a7e42b81b28db6ee4b7bf5eeb8b5f1
Gerrit-Change-Number: 36610
Gerrit-PatchSet: 2
Gerrit-Owner: pespin <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: osmith <[email protected]>
Gerrit-Reviewer: pespin <[email protected]>
Gerrit-MessageType: merged

Reply via email to