TFTF: Use CACTUS_ECHO_CMD in direct message tests

There were two very similar tests, that were targeted to the same
purpose. This patch keeps the logic using an echo value, as it was being
used in tests involving SPs.

Signed-off-by: J-Alves <joao.alves@arm.com>
Change-Id: Ib278d28098e60dddf807b267951f7e2b8762e9e3
diff --git a/spm/cactus/cactus_main.c b/spm/cactus/cactus_main.c
index 37b8bd5..650f06d 100644
--- a/spm/cactus/cactus_main.c
+++ b/spm/cactus/cactus_main.c
@@ -41,9 +41,7 @@
 static void __dead2 message_loop(ffa_vm_id_t vm_id, struct mailbox_buffers *mb)
 {
 	smc_ret_values ffa_ret;
-	uint32_t sp_response;
 	ffa_vm_id_t destination;
-	uint64_t cactus_cmd;
 
 	/*
 	* This initial wait call is necessary to inform SPMD that
@@ -54,8 +52,6 @@
 	ffa_ret = ffa_msg_wait();
 
 	for (;;) {
-		/* temporary 'skip_switch' label. Deleted in following commit */
-		skip_switch:
 		VERBOSE("Woke up with func id: %x\n", ffa_func_id(ffa_ret));
 
 		if (ffa_func_id(ffa_ret) == FFA_ERROR) {
@@ -80,25 +76,7 @@
 
 		PRINT_CMD(ffa_ret);
 
-		cactus_cmd = cactus_get_cmd(ffa_ret);
-
-		if (cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
-			goto skip_switch;
-		}
-		switch (cactus_cmd) {
-		default:
-			/*
-			 * Currently direct message test is handled here.
-			 * TODO: create a case within the switch case
-			 * For the sake of testing, add the vm id to the
-			 * received message.
-			 */
-			sp_response = ffa_ret.ret3 | vm_id;
-			VERBOSE("Replying with direct message response: %x\n", sp_response);
-			ffa_ret = ffa_msg_send_direct_resp(vm_id,
-							   HYP_ID,
-							   sp_response);
-
+		if (!cactus_handle_cmd(&ffa_ret, &ffa_ret, mb)) {
 			break;
 		}
 	}
diff --git a/spm/cactus/cactus_test_cmds.c b/spm/cactus/cactus_test_cmds.c
index 128f4c0..e13e40d 100644
--- a/spm/cactus/cactus_test_cmds.c
+++ b/spm/cactus/cactus_test_cmds.c
@@ -21,12 +21,14 @@
 bool cactus_handle_cmd(smc_ret_values *cmd_args, smc_ret_values *ret,
 		       struct mailbox_buffers *mb)
 {
+	uint64_t in_cmd;
+
 	if (cmd_args == NULL || ret == NULL) {
 		ERROR("Invalid argumentos passed to %s!\n", __func__);
 		return false;
 	}
 
-	uint64_t in_cmd = cactus_get_cmd(*cmd_args);
+	in_cmd = cactus_get_cmd(*cmd_args);
 
 	for (struct cactus_cmd_handler *it_cmd = cactus_cmd_handler_begin;
 	     it_cmd < cactus_cmd_handler_end;
@@ -37,5 +39,8 @@
 		}
 	}
 
-	return false;
+	*ret = cactus_error_resp(ffa_dir_msg_dest(*cmd_args),
+				 ffa_dir_msg_source(*cmd_args),
+				 CACTUS_ERROR_UNHANDLED);
+	return true;
 }
diff --git a/spm/cactus/cactus_test_cmds.h b/spm/cactus/cactus_test_cmds.h
index d8bba95..f892a2c 100644
--- a/spm/cactus/cactus_test_cmds.h
+++ b/spm/cactus/cactus_test_cmds.h
@@ -23,6 +23,7 @@
 #define CACTUS_ERROR_INVALID		U(1)
 #define CACTUS_ERROR_TEST		U(2)
 #define CACTUS_ERROR_FFA_CALL		U(3)
+#define CACTUS_ERROR_UNHANDLED		U(4)
 
 /**
  * Get command from struct smc_ret_values.
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c b/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c
index 50e8582..72daae4 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c
@@ -18,37 +18,26 @@
 #define ECHO_VAL2 U(0xb0b0b0b0)
 #define ECHO_VAL3 U(0xc0c0c0c0)
 
-#define DIRECT_MSG_TEST_PATTERN1	(0xaaaa0000)
-#define DIRECT_MSG_TEST_PATTERN2	(0xbbbb0000)
-#define DIRECT_MSG_TEST_PATTERN3	(0xcccc0000)
-
 static const struct ffa_uuid expected_sp_uuids[] = {
 		{PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID}
 	};
 
-static test_result_t send_receive_direct_msg(unsigned int sp_id,
-					     unsigned int test_pattern)
+static test_result_t send_cactus_echo_cmd(ffa_vm_id_t sender,
+					  ffa_vm_id_t dest,
+					  uint64_t value)
 {
-	smc_ret_values ret_values;
+	smc_ret_values ret;
+	ret = cactus_echo_send_cmd(sender, dest, value);
 
-	/* Send a message to SP through direct messaging */
-	ret_values = ffa_msg_send_direct_req(HYP_ID, sp_id, test_pattern);
-
-	/*
-	 * Return responses may be FFA_MSG_SEND_DIRECT_RESP or FFA_INTERRUPT,
-	 * but only expect the former. Expect SMC32 convention from SP.
-	 */
-	if (!is_ffa_direct_response(ret_values)) {
-		tftf_testcase_printf("ffa_msg_send_direct_req returned %x\n",
-				     ffa_func_id(ret_values));
+	if (!is_ffa_direct_response(ret)) {
+		ERROR("Failed to send message. error: %x\n",
+		      ffa_error_code(ret));
 		return TEST_RESULT_FAIL;
 	}
 
-	/*
-	 * Message loop in SP returns initial message with the running VM id
-	 * into the lower 16 bits of initial message.
-	 */
-	if (ret_values.ret3 != (test_pattern | sp_id)) {
+	if (cactus_get_response(ret) != CACTUS_SUCCESS ||
+	    cactus_echo_get_val(ret) != value) {
+		ERROR("Echo Failed!\n");
 		return TEST_RESULT_FAIL;
 	}
 
@@ -67,7 +56,7 @@
 	/**********************************************************************
 	 * Send a message to SP1 through direct messaging
 	 **********************************************************************/
-	result = send_receive_direct_msg(SP_ID(1), DIRECT_MSG_TEST_PATTERN1);
+	result = send_cactus_echo_cmd(HYP_ID, SP_ID(1), ECHO_VAL1);
 	if (result != TEST_RESULT_SUCCESS) {
 		return result;
 	}
@@ -75,7 +64,7 @@
 	/**********************************************************************
 	 * Send a message to SP2 through direct messaging
 	 **********************************************************************/
-	result = send_receive_direct_msg(SP_ID(2), DIRECT_MSG_TEST_PATTERN2);
+	result = send_cactus_echo_cmd(HYP_ID, SP_ID(2), ECHO_VAL2);
 	if (result != TEST_RESULT_SUCCESS) {
 		return result;
 	}
@@ -83,7 +72,7 @@
 	/**********************************************************************
 	 * Send a message to SP1 through direct messaging
 	 **********************************************************************/
-	result = send_receive_direct_msg(SP_ID(1), DIRECT_MSG_TEST_PATTERN3);
+	result = send_cactus_echo_cmd(HYP_ID, SP_ID(1), ECHO_VAL3);
 
 	return result;
 }