/**
 * @file   drMain.c
 * @brief  Implements the entry point of the driver.
 *
  * <Copyright goes here>
 */

#include "drStd.h"
#include "DrApi/DrApiMm.h"
#include "DrApi/DrApiThread.h"
#include "DrApi/DrApiIpcMsg.h"
#include "DrApi/DrApiLogging.h"

#include "drCommon.h"

#include "sec_fr_common.h"
#include "sec_fr_pal_common.h"

DECLARE_DRIVER_MAIN_STACK(1024*1024)

#define UNUSED(a) do { (void)(a); } while (0)

/* External functions */
extern void drIpchInit(void);
extern _NORETURN void drExchLoop(void);


/**
 * Initialization function
 */
static drApiResult_t doInitialization( void ) {
    drApiResult_t ret = E_OK;

    /**
     * TODO: This is the function where certain initialization
     * can be done before proceeding further. Such as HW related
     * initialization. Update the return code accordingly
     */

    return ret;
}

/**
 * Main routine.
 * Starts the required threads.
 */
_DRAPI_ENTRY void drMain(const addr_t dciBuffer, const uint32_t dciBufferLen) {
    UNUSED(dciBuffer);
    UNUSED(dciBufferLen);

    /* Check version */

    PAL_LOGV("[Secure FR driver] %d.%d, Build " __DATE__ ", " __TIME__ EOL, DRIVER_VERSION_MAJOR, DRIVER_VERSION_MINOR);

    /* Initialization */
    if (E_OK != doInitialization()) {
        PAL_LOGE("[Secure FR driver] drMain(): Initialization failed");

        /* No need to proceed further. Shutdown the main thread */
        if (E_OK != drApiStopThread(NILTHREAD))	{
            PAL_LOGE("[Secure FR driver] drMain(): Unable to stop main thread");
        }
    }

    /* Start IPC handler */
    drIpchInit();

    /* Start exception handler */
    drExchLoop();
}
