/*
* Copyright (c) 2013 TRUSTONIC LIMITED
* All rights reserved
*
* The present software is the confidential and proprietary information of
* TRUSTONIC LIMITED. You shall not disclose the present software and shall
* use it only in accordance with the terms of the license agreement you
* entered into with TRUSTONIC LIMITED. This software may be subject to
* export or import laws in certain countries.
*/
#include <stdint.h>
#include "tlStd.h"
#include "TlApi/TlApi.h"
#include "stack_protection.h"

#include "skpm_cmd_handler.h"

DECLARE_TRUSTLET_MAIN_STACK(300000)
#if defined USE_SCRYPTO || USE_TBASE_API_LEVEL_11
void __use_no_semihosting(void){}
#else
DECLARE_TRUSTLET_MAIN_HEAP(300000)
#endif

#ifdef USE_SCRYPTO
char TZ_APP_NAME[] = { "skpm" };
#endif


/**
* Trusted Application entry.
*/
_TLAPI_ENTRY void tlMain( const addr_t tciBuffer, const uint32_t tciBufferLen ) {
    stack_protection_init();

    p_cmd_t pCmd = (p_cmd_t) tciBuffer;
    p_rsp_t pRsp = (p_rsp_t) tciBuffer;

    if ((pCmd == NULL) || (pRsp == NULL) || (tciBufferLen != sizeof(cmd_t)) || (tciBufferLen != sizeof(rsp_t))) {
        LOGE("Invalid pCmd or pRsp");
        pRsp->ret = -1;
        pRsp->status = -1;
        goto error;
    }

    LOGD("SKPM Trusted Application is waiting for a notification to arrive");
    for (;;) {
        /* Wait for a notification to arrive */
        tlApiWaitNotification(TLAPI_INFINITE_TIMEOUT);

        cmdHandler(pCmd->cmd_id, pCmd, pRsp);
error:
        /* Notify back the TLC */
        tlApiNotify();
    }
}
