#include "sec_mw.h"
#include "pearlv3_ese_sle97.h"
#include "spi_state.h"
#include "tz_debug.h"

static uint8_t spi_mode0_enabled = 0;

ESESTATUS iso7816_init(void)
{
    int result = ESESTATUS_FAILED;
    uint8_t get_capabilities[] = {0x80, 0x01, 0x10, 0x86, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    uint8_t spi_rcv[259] = {0,};

    uint16_t len = 5;
    uint16_t timeout = 5;

    spi_mode0_enabled = 1;

    result = spi_send_raw_activepolling_mode(get_capabilities, spi_rcv, len, timeout, MODE_TRANSCIEVE);
    if (0 != result)
    {
        LOGE("get capa failed! result of getcapa = %d", result);
        return ESESTATUS_GET_CAPA_ERROR;
    }
    LOGD("getCapa Success");

    return ESESTATUS_SUCCESS;
}

void iso7816_deinit(void)
{
    (void) 0; // This function is not available in OT API therefore do nothing
}

ESESTATUS iso7816_transceive(secEse_7816_cpdu_t *capdu, secEse_7816_rpdu_t *rapdu)
{
    return phOTEseSLE97_7816_Transceive((p_secEse_7816_cpdu_t) capdu, (p_secEse_7816_rpdu_t) rapdu);
}

ESESTATUS iso7816_apdu_transceive(uint16_t apdu_len, uint8_t *p_apdu, p_secEse_7816_rpdu_t p_rapdu)
{
    return phOTEseSLE97_7816_APDU_Transceive(apdu_len, p_apdu, (p_secEse_7816_rpdu_t) p_rapdu);
}

uint8_t iso7816_spi_mode0(void)
{
    return spi_mode0_enabled;
}
