/* =================================================================================
 *  Copyright (c) 2020 by Qualcomm Technologies, Incorporated.  All Rights Reserved.
 * =================================================================================
 */

/* Included Files */
#include <stdint.h>
#include <stdlib.h>
#include <eid_ta_defs.h>
#include <eid_impl.h>
#include <tee_internal_api.h>
#include <qsee_log.h>

/**
  @brief
    Add any app specific initialization code here
    QSEE will call this function after secure app is loaded and
    authenticated
*/
TEE_Result
TA_CreateEntryPoint(
  void)
{
   /* INITIALIZE APPLICATION INSTANCE */
   qsee_log_set_mask(QSEE_LOG_MSG_DEBUG | QSEE_LOG_MSG_ERROR | QSEE_LOG_MSG_FATAL);

   SLogTrace("TA_CreateEntryPoint()");

   return TEE_SUCCESS;
}

/**
  @brief
    App specific shutdown code
    App will be given a chance to shutdown gracefully
*/
void
TA_DestroyEntryPoint(
  void)
{
   /* SHUTDOWN APPLICATION INSTANCE */
   
   SLogTrace("TA_DestroyEntryPoint()");
}


/**
   @brief
     Add any session-specific initialization code here.
     App may use pSessionContext to save state of session.
 */
TEE_Result
TA_OpenSessionEntryPoint(
  uint32_t  nParamTypes,
  TEE_Param pParams[4],
  void**    ppSessionContext )
{
   TEE_Result result = TEE_ERROR_GENERIC;
   SLogTrace("TA_OpenSessionEntryPoint()");

   do
   {
      (*ppSessionContext) = TEE_Malloc(sizeof(uint32_t), 0);
      if (*ppSessionContext == NULL)
      {
         SLogError("Failed to allocate session context");
         result = TEE_ERROR_OUT_OF_MEMORY;
         break;
      }
      result = TEE_SUCCESS;
   } while (0);

   SLogTrace("result=%X", result);

   return result;
}

/**
   @brief
     Add any session-specific shutdown code here.
     This is the last function called before the session is terminated.
 */
void
TA_CloseSessionEntryPoint(
  void* pSessionContext)
{
   /* SESSION IS CLOSED, RELEASE ALL DEPENDENCIES */
   SLogTrace("TA_CloseSessionEntryPoint()");

   if (pSessionContext)
   {
      TEE_Free(pSessionContext);
      pSessionContext = NULL;
   }
}

/**
   @brief
     This is the App's command handler.
     Commands are issued with context to one particular session.
 */
TEE_Result
TA_InvokeCommandEntryPoint(
  void*     pSessionContext,
  uint32_t  nCommandID,
  uint32_t  nParamTypes,
  TEE_Param pParams[4])
{
   TEE_Result result = TEE_ERROR_GENERIC;

   SLogTrace("TA_InvokeCommandEntryPoint()");
   SLogTrace("appCommandId[0x%08X], nParamTypes[0x%08X]", nCommandID, nParamTypes);
  
   do
   {
      switch (nCommandID)
      {
         case CMD_HAS_EID_CERTIFICATE:
            result = haseIDCertificate(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_EID_STATE:
            result = geteIDState (pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_NONE_SECURE_FACE_DATA:
            result = getNoneSecureFaceData(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_APPLY_EID_CERTIFICATE:
            result = applyeIDCertificate(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_INSTALL_EID_CERTIFICATE:
            result = installeIDCertificate(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_EID_INFO:
            result = geteIDInfo(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_INIT_SIGN:
            result = initSign(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_FINISH_SIGN:
            result = finishSign(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_ABORT_SIGN:
            result = abortSign(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_EID_CARD:
            result = geteIDIDCard(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_CLEAR:
            result = clear(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_RANDOM_DATA:
            result = getRandomData(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_SECURE_IDDATA:
            result = geteIDSecureIDData(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_GET_DESENSITIZED_DATA:
            result = geteIDDesensitizedIDData(pSessionContext, nParamTypes, pParams);
            break;
         case CMD_PROCESS_COMMAND:
            result = processCommand(pSessionContext, nParamTypes, pParams);
            break;
         default:
            SLogError("unsupported command!");
            result = EID_ERROR_UNSUPPORTED_COMMAND;
      }
   } while (0);

   SLogTrace("result=%X", result);

   return result;
}

