/*
 * @file app_main.c
 * @brief MST handler entry point
 * Copyright (c) 2015, Samsung Electronics Corporation. All rights reserved.
 */
#include <comdef.h>
#include "qsee_log.h"
#include "app_main.h"

#include "tlc_tz_mst.h"

extern void mst_cmd_handler(void* cmd, uint32 cmdlen, void* rsp, uint32 rsplen);

/**
 * Modify the app name to your specific app name
 */
char TZ_APP_NAME[] = {"mst"};

/**
  @brief
    Add any app specific initialization code here
    QSEE will call this function after secure app is loaded and
    authenticated
*/
void tz_app_init(void)
{
  /*  App specific initialization code*/
  qsee_log(QSEE_LOG_MSG_FATAL, "MST Init ");
}

/**
  @brief
    App specific command handler
    App executes code based on input command
*/
void tz_app_cmd_handler(void* cmd, uint32 cmdlen,
			void* rsp, uint32 rsplen)
{
	uint32 cmd_id = 0;

	qsee_log(QSEE_LOG_MSG_FATAL, "tz_app_cmd_handler");

	if ((NULL == cmd) || (NULL == rsp)) {
		qsee_log(QSEE_LOG_MSG_FATAL,
			 "tz_app_cmd_handler invalid input");
		return;
	}

	if (cmdlen < sizeof(mst_req_t)) {
		qsee_log(QSEE_LOG_MSG_FATAL,
			 "cmdlen = %u < %u = sizeof(mst_req_t)",
			 cmdlen, sizeof(mst_req_t));
		return;
	}

	if (rsplen < sizeof(mst_rsp_t)) {
		qsee_log(QSEE_LOG_MSG_FATAL,
			 "rsplen = %u < %u = sizeof(mst_rsp_t)",
			 rsplen, sizeof(mst_rsp_t));
		return;
	}

	// First 4 bytes are always command id
	cmd_id = (*(uint32*)cmd);

	if (SVC_MST_ID == (cmd_id & 0xFFFF0000)) {
		mst_cmd_handler(cmd, cmdlen, rsp, rsplen);
	} else {
		qsee_log(QSEE_LOG_MSG_FATAL,
			 "invalid command id: %u", cmd_id);
		return;
	}

	return;
}


/**
  @brief
    App specific shutdown
    App will be given a chance to shutdown gracefully
*/
void tz_app_shutdown(void)
{
  /* app specific shutdown code*/
  qsee_log(QSEE_LOG_MSG_DEBUG, "MST shutdown");
  return;
}
