
/*
 * =====================================================================================
 *
 *       Filename:  app_main.c
 *
 *    Description:  PEBBLE main
 *
 *        Version:  1.0
 *        Created:  09/16/2019 15:26:11 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *        Company:  Samsung Electronics
 *        Copyright (c) 2015 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

/** Inlcudes */
#include "process_cmd.h"
#include <comdef.h>
#include "qsee_log.h"
#include "tz_pebble_interface.h"
#include "pebble_defs.h"
#include "tci.h"

/** App Name */
char TZ_APP_NAME[] = {"pebble"};

/**
 * @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_DEBUG, "pebble Init");
}

/**
 * @brief
 * App specific command handler
 * App executes code based on input command
 *
 * @param[in] cmd     - command
 * @param[in] cmdlen  - command length
 * @param[out] *rsp   - response
 * @param[out] rsplen - response length
 */
void tz_app_cmd_handler(void *cmd, uint32 cmdlen, void *rsp, uint32 rsplen) {
        pebble_return_code_t ret = PEBBLE_STATUS_SUCCESS;
        uint32_t commandId = 0;

        tci_message_t sendmsg;
        tci_message_t respmsg;

        if (NULL == cmd || NULL == rsp || cmdlen < sizeof(tci_message_t)
                || ((cmd < rsp) && ((void *)((uint8_t *)cmd + cmdlen) > rsp))
                || ((rsp < cmd) && ((void *)((uint8_t *)rsp + rsplen) > cmd))) {
                        PEBBLE_LOG_DEBUG("APP_MAIN: cmd or rsp buffer too small, exiting from TA - %d,%d,%d", cmdlen, rsplen, sizeof(tci_message_t));
                        PEBBLE_LOG_DEBUG("APP_MAIN: or cmd overlap with resp - %p,%d,%p,%d", cmd, cmdlen, rsp, rsplen);
                        PEBBLE_LOG("APP_MAIN: fail to setup command handler");

                        ret = PEBBLE_STATUS_FAIL;
                        goto exit;
        }
        TEE_MemMove(&sendmsg, cmd, sizeof(tci_message_t));
        TEE_MemMove(&respmsg, rsp, sizeof(tci_message_t));

        commandId = sendmsg.header.id;
        PEBBLE_LOG_DEBUG("\ncommand id - %08X", commandId);

        if (!IS_CMD(commandId)) {
                PEBBLE_LOG("Invalid command id, exiting!");

                ret = PEBBLE_STATUS_FAIL;
                goto exit;
        }

        ret = process_cmd(commandId, &sendmsg, &respmsg);

exit:
        respmsg.header.id = RSP_ID(commandId);
        respmsg.header.status = ret;
        TEE_MemMove(cmd, &sendmsg, sizeof(tci_message_t));
        TEE_MemMove(rsp, &respmsg, sizeof(tci_message_t));

        /*TODO: HVC */
}


/**
 * @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, "pebble shutdown");
        return;
}
