asterisk/AMI_Functions: improvements to implement PJSIPRegister

Change-Id: Ibc4ff6eaad84f67d33c1211dc5e39294ce1c8fac
diff --git a/asterisk/AMI_Functions.ttcn b/asterisk/AMI_Functions.ttcn
index d5139f5..72420e1 100644
--- a/asterisk/AMI_Functions.ttcn
+++ b/asterisk/AMI_Functions.ttcn
@@ -25,10 +25,14 @@
 }
 
 const charstring AMI_FIELD_ACTION := "Action";
+const charstring AMI_FIELD_ACTION_ID := "ActionId";
 const charstring AMI_FIELD_USERNAME := "Username";
 const charstring AMI_FIELD_SECRET := "Secret";
 const charstring AMI_FIELD_RESPONSE := "Response";
 
+/* Extensions: */
+const charstring AMI_FIELD_REGISTRATION := "Registration";
+
 type record AMI_Field {
 	charstring	key,
 	charstring	val
@@ -69,28 +73,46 @@
 template (value) AMI_Field
 ts_AMI_Field_Action(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_ACTION, val);
 template (value) AMI_Field
+ts_AMI_Field_ActionId(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_ACTION_ID, val);
+template (value) AMI_Field
 ts_AMI_Field_Username(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_USERNAME, val);
 template (value) AMI_Field
 ts_AMI_Field_Secret(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_SECRET, val);
+/* Extensions: */
+template (value) AMI_Field
+ts_AMI_Field_Registration(template (value) charstring val) := ts_AMI_Field(AMI_FIELD_REGISTRATION, val);
 
 template (present) AMI_Field
 tr_AMI_Field_Action(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_ACTION, val);
 template (present) AMI_Field
+tr_AMI_Field_ActionId(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_ACTION_ID, val);
+template (present) AMI_Field
 tr_AMI_Field_Username(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_USERNAME, val);
 template (present) AMI_Field
 tr_AMI_Field_Secret(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_SECRET, val);
 template (present) AMI_Field
 tr_AMI_Field_Response(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_RESPONSE, val);
+/* Extensions: */
+template (present) AMI_Field
+tr_AMI_Field_Registration(template (present) charstring val := ?) := tr_AMI_Field(AMI_FIELD_REGISTRATION, val);
 
 
 template (present) AMI_Field
 tr_AMI_Field_ResponseSuccess := tr_AMI_Field(AMI_FIELD_RESPONSE, "Success");
 
 
-/*
+/***********************
  * Message Templates:
+ ***********************/
+
+/*
+ * ACTIONS
  */
 
+/* Action: Login
+ * Username: <value>
+ * Secret: <value>
+ */
 template (value) AMI_Msg
 ts_AMI_Action_Login(charstring username, charstring secret) := {
 	ts_AMI_Field_Action("Login"),
@@ -106,15 +128,101 @@
 	tr_AMI_Field_Secret(secret)
 );
 
+/* Action: PJSIPRegister
+ * ActionID: <value>
+ * Registration: volte_ims
+ */
+template (value) AMI_Msg
+ts_AMI_Action_PJSIPRegister(template (value) charstring registration := "volte_ims",
+			    template (value) charstring action_id := "0001") := {
+	ts_AMI_Field_Action("PJSIPRegister"),
+	ts_AMI_Field_ActionId(action_id),
+	ts_AMI_Field_Registration(registration)
+};
+template (present) AMI_Msg
+tr_AMI_Action_PJSIPRegister(template (present) charstring registration := ?,
+			    template (present) charstring action_id := ?) := {
+	tr_AMI_Field_Action("PJSIPRegister"),
+	tr_AMI_Field_ActionId(action_id),
+	tr_AMI_Field_Registration(registration)
+};
+
+/*
+ * RESPONSES
+ */
+
+/* Response: Success
+ */
 template (present) AMI_Msg
 tr_AMI_Response_Success := superset(
 	tr_AMI_Field_ResponseSuccess
 );
 
+/* Response: Success
+ * ActionId: <value>
+ */
+template (present) AMI_Msg
+tr_AMI_Response_Success_ActionId(template (present) charstring action_id := ?) := superset(
+	tr_AMI_Field_ResponseSuccess,
+	tr_AMI_Field_ActionId(action_id)
+);
+
 /*
  * Functions:
  */
 
+/* Generate a random "ActionId" value: */
+function f_gen_action_id() return charstring {
+	return hex2str(f_rnd_hexstring(16));
+}
+
+function f_ami_msg_find(AMI_Msg msg,
+			template (present) charstring key := ?)
+return template (omit) AMI_Field {
+	var integer i;
+
+	for (i := 0; i < lengthof(msg); i := i + 1) {
+		if (not ispresent(msg[i])) {
+			continue;
+		}
+		if (match(msg[i].key, key)) {
+			return msg[i];
+		}
+	}
+	return omit;
+}
+
+function f_ami_msg_find_or_fail(AMI_Msg msg,
+				template (present) charstring key := ?)
+return AMI_Field {
+	var template (omit) AMI_Field field;
+	field := f_ami_msg_find(msg, key);
+	if (istemplatekind(field, "omit")) {
+		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail,
+			log2str("Key ", key, " not found in ", msg));
+	}
+	return valueof(field);
+}
+
+function f_ami_msg_get_value(AMI_Msg msg,
+			     template (present) charstring key := ?)
+return template (omit) charstring {
+	var template (omit) AMI_Field field;
+	field := f_ami_msg_find(msg, key);
+	if (istemplatekind(field, "omit")) {
+		return omit;
+	}
+	return field.val;
+}
+
+function f_ami_msg_get_value_or_fail(AMI_Msg msg,
+				     template (present) charstring key := ?)
+return template charstring {
+	var AMI_Field field;
+	field := f_ami_msg_find_or_fail(msg, key);
+	return field.val;
+}
+
 private function f_ami_wait_for_prompt_str(TELNETasp_PT pt, charstring log_label := "(?)")
 return charstring {
 	var charstring rx, buf := "";
@@ -172,11 +280,23 @@
 
 function f_ami_transceive_match_response_success(TELNETasp_PT pt,
 						 template (value) AMI_Msg tx_msg) {
-	f_ami_transceive_match(pt, tx_msg, tr_AMI_Response_Success);
+	var template (present) AMI_Msg exp_resp;
+	var template (omit) charstring action_id := f_ami_msg_get_value(valueof(tx_msg), AMI_FIELD_ACTION_ID);
+	if (isvalue(action_id)) {
+		exp_resp := tr_AMI_Response_Success_ActionId(action_id);
+	} else {
+		exp_resp := tr_AMI_Response_Success;
+	}
+	f_ami_transceive_match(pt, tx_msg, exp_resp);
 }
 
 function f_ami_action_login(TELNETasp_PT pt, charstring username, charstring secret) {
 	f_ami_transceive_match_response_success(pt, ts_AMI_Action_Login(username, secret));
 }
 
+function f_ami_action_PJSIPRegister(TELNETasp_PT pt, charstring register) {
+	var charstring reg_action_id := f_gen_action_id();
+	f_ami_transceive_match_response_success(pt, ts_AMI_Action_PJSIPRegister(register, reg_action_id));
+}
+
 }