#include "scrs.h"

#include "sec_apdu.h"
#include "tz_debug.h"

void deleteDefaultCard(p_cmd_t cmd, p_rsp_t rsp) {

    secEse_7816_rpdu_t rpdu;
    secEse_7816_cpdu_t cpdu;
    uint8_t r_data[MAX_RAPDU_DATA_SIZE];
    ESESTATUS ese_status = 0;
    uint8_t scrs_aid[] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
    uint8_t cpduData[MAX_CAPDU_DATA_SIZE] = {0,};
    uint32_t cpduLen = 0, inputSize;

    LOGI("Delete Default Card");

    memset(&cpdu, 0, sizeof(secEse_7816_cpdu_t));
    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    memset(r_data, 0, MAX_RAPDU_DATA_SIZE);

    inputSize = cmd->dataLen;
    if (inputSize > sizeof(cpduData)) {
        LOGE("cpduData overflow error");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
    memcpy(cpduData, cmd->data, inputSize);

    cpduLen = inputSize;
    rpdu.pdata = r_data;

    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    rpdu.pdata = r_data;

    if (ESESTATUS_SUCCESS != secEseSelect(0x00, scrs_aid, 0, 0x09, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE( "get Delete Card failed to SELECT scrs - ESESTATUS = 0x%08X", ese_status );
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }

    cpdu.cla = 0x80;
    cpdu.ins = 0xF0;
    cpdu.p1  = 0x28;
    cpdu.p2  = 0x00;
    cpdu.lc  = cpduLen;
    cpdu.pdata = cpduData;
    cpdu.le_type = 0x00;

    if (ESESTATUS_SUCCESS != secEseTransmit(0x00, &cpdu, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE("get Delete Card failed to send apdu");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }

    /* Note:  rpdu.len is uint16 anc can't be > MAX_DATA_SIZE */
    memcpy(rsp->data, rpdu.pdata, rpdu.len);
    rsp->dataLen = rpdu.len;
    if(rpdu.len+1 > (signed)sizeof(rsp->data)) {
        LOGE("deleteDefaultCard data Overflow");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
    rsp->data[rpdu.len] = rpdu.sw1;
    rsp->data[rpdu.len+1] = rpdu.sw2;
    rsp->ret = RET_SUCCESS;

    hex_print_tag_debug("Delete default Card :: ", rsp->data, rsp->dataLen+2);

    LOGD("Delete default Card result : 0x%02x %02x", rpdu.sw1, rpdu.sw2);

error_deleteCard:
    LOGE("Delete default Card returned: %d", rsp->ret);

}

void getDefaultCardAid(p_cmd_t cmd, p_rsp_t rsp) {
    secEse_7816_rpdu_t rpdu;
    secEse_7816_cpdu_t cpdu;
    uint8_t r_data[MAX_RAPDU_DATA_SIZE];
    ESESTATUS ese_status = 0;

    uint8_t scrs_aid[] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
    uint8_t cpduData[MAX_CAPDU_DATA_SIZE] = {0,};
    uint32_t cpduLen = 0, inputSize;

    LOGI("Get Default Card AID");

    memset(&cpdu, 0, sizeof(secEse_7816_cpdu_t));
    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    memset(r_data, 0, MAX_RAPDU_DATA_SIZE);

    inputSize = cmd->dataLen;
    if (inputSize > sizeof(cpduData)) {
        LOGE("cpduData overflow error");
        rsp->ret = RET_ERR_GET_WALLET_AID;
        goto error_getDefaultCard;
    }
    memcpy(cpduData, cmd->data, inputSize);

    cpduLen = inputSize;
    rpdu.pdata = r_data;

    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    rpdu.pdata = r_data;

    LOGD("select SCRS");
    if (ESESTATUS_SUCCESS != secEseSelect(0x00, scrs_aid, 0, 0x09, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE( "get Default Card AID failed to SELECT scrs - ESESTATUS = 0x%08X", ese_status );
        rsp->ret = RET_ERR_GET_WALLET_AID;
    }

    cpdu.cla = 0x80;
    cpdu.ins = 0xF2;
    cpdu.p1  = 0x28;
    cpdu.p2  = 0x00;
    cpdu.lc  = cpduLen;
    cpdu.pdata = cpduData;
    cpdu.le_type = 0x00;

    LOGD("send get default card aid apdu");
    hex_print_tag_debug("get default card cpduData :: ", cpduData, cpduLen);

    if (ESESTATUS_SUCCESS != secEseTransmit(0x00, &cpdu, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE("get Default Card AID failed to send apdu");
        rsp->ret = RET_ERR_GET_WALLET_AID;
        goto error_getDefaultCard;
    }

    /* Note:  rpdu.len is uint16 anc can't be > MAX_DATA_SIZE */
    memcpy(rsp->data, rpdu.pdata, rpdu.len);
    rsp->dataLen = rpdu.len;
    if(rpdu.len+1 > (signed)sizeof(rsp->data)) {
        LOGE("get Default Card AID data Overflow");
        rsp->ret = RET_ERR_GET_WALLET_AID;
        goto error_getDefaultCard;
    }
    rsp->data[rpdu.len] = rpdu.sw1;
    rsp->data[rpdu.len+1] = rpdu.sw2;
    rsp->ret = RET_SUCCESS;

    hex_print_tag_debug("Get Default Card AID :: ", rsp->data, rsp->dataLen+2);
    LOGD("Get default Card AID result : 0x%02x %02x", rpdu.sw1, rpdu.sw2);

error_getDefaultCard:
    LOGE("Get default Card AID returned: %d", rsp->ret);
}

void getDefaultCardPackageList(p_rsp_t rsp) {
    uint8_t channel = 0;
    uint8_t rdata[255] = {0,};
    secEse_7816_rpdu_t rsp_data;
    secEse_7816_cpdu_t cmd_data;
    uint8_t scrs_aid[] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
    ESESTATUS result = ESESTATUS_OK;

    result = secEseOpen(&channel);
    if (result != ESESTATUS_OK){
        LOGE("getDefaultCardPackage secEseOpen fail %04x", result);
        rsp->ret = RET_ERR_GET_WALLET_PACKAGE;
        goto error_getDefaultCardList;
    } else {
        LOGD("getDefaultCardPackage secEseOpen success %04x", result);
    }

    memset(&rsp_data, 0, sizeof(secEse_7816_rpdu_t));
    rsp_data.pdata = rdata;
    //result = secEseSelect(channel,SCRS_AID,0,9,&rsp_data);
    result = secEseSelect(channel,scrs_aid,0,0x09,&rsp_data);
    if (result != ESESTATUS_OK){
        LOGE("getDefaultCardPackage secEseSelect fail %04x", result);
        rsp->ret = RET_ERR_GET_WALLET_PACKAGE;
        goto error_getDefaultCardList;
    } else {
        LOGD("getDefaultCardPackage secEseSelect success %04x", result);
    }

    memset(&rsp_data, 0, sizeof(secEse_7816_rpdu_t));
    memset(&cmd_data, 0, sizeof(secEse_7816_cpdu_t));

    cmd_data.cla = 0x80;
    cmd_data.ins = 0xF2;
    cmd_data.p1 = 0x18;
    cmd_data.p2 = 0x00;
    cmd_data.lc = 0x00;
    cmd_data.le = 0x00;
    cmd_data.le_type = 0x01;
    cmd_data.pdata = NULL;
    cmd_data.cpdu_type = 0x00;

    rsp_data.pdata = rdata;

    result = secEseTransmit(channel,&cmd_data,&rsp_data);
    if (result != ESESTATUS_OK){
        LOGE("getDefaultCardPackage secEseTransmit fail %04x", result);
        rsp->ret = RET_ERR_GET_WALLET_PACKAGE;
        goto error_getDefaultCardList;
    } else {
        LOGD("getDefaultCardPackage secEseTransmit success %04x", result);
    }
    result = secEseClose(channel);
    if (result != ESESTATUS_OK){
        LOGE("getDefaultCardPackage secEseClose fail %04x", result);
        rsp->ret = RET_ERR_GET_WALLET_PACKAGE;
        goto error_getDefaultCardList;
    } else {
        LOGD("getDefaultCardPackage secEseClose success %04x", result);
    }

    if (result == ESESTATUS_OK){
        /* Note:  rpdu.len is uint16 anc can't be > MAX_DATA_SIZE */
        memcpy(rsp->data, rsp_data.pdata, rsp_data.len);
        rsp->dataLen = rsp_data.len;
        rsp->ret = RET_SUCCESS;
        LOGD("getDefaultCardPackage return RET_SUCCESS");
    }

error_getDefaultCardList:
    LOGE("Get default Card AID returned: %d", rsp->ret);
}

void getRFActivationList(p_rsp_t rsp) {
    uint8_t channel = 0;
    uint8_t rdata[500] = {0,};
    secEse_7816_rpdu_t rsp_data;
    secEse_7816_cpdu_t cmd_data;
    uint8_t scrs_aid[] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
    uint8_t apdu_get_status[7] = { 0x4F, 0x00, 0x5C, 0x03, 0x4F, 0x9F, 0x70}; //4F005C034F9F70
    int offset = 0;
    int i=0;
    ESESTATUS result = ESESTATUS_OK;
    rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;

    //eSE Open
    result = secEseOpen(&channel);
    if (result != ESESTATUS_OK){
        LOGE("getRFActivationList secEseOpen fail %04x", result);
        rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;
        goto error;
    } else {
        LOGD("getRFActivationList secEseOpen success %04x", result);
    }

    //select SRCS
    memset(&rsp_data, 0, sizeof(secEse_7816_rpdu_t));
    rsp_data.pdata = rdata;
    //result = secEseSelect(channel,SCRS_AID,0,9,&rsp_data);
    result = secEseSelect(channel,scrs_aid,0,0x09,&rsp_data);
    if (result != ESESTATUS_OK){
        LOGE("getRFActivationList secEseSelect fail %04x", result);
        rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;
        goto error;
    } else {
        LOGD("getRFActivationList secEseSelect success %04x", result);
    }

    memset(&rsp_data, 0, sizeof(secEse_7816_rpdu_t));
    memset(&cmd_data, 0, sizeof(secEse_7816_cpdu_t));

    //transmit get status
    //send 80F24000#(4F005C#(4F9F70))00
    cmd_data.cla = 0x80;
    cmd_data.ins = 0xF2;
    cmd_data.p1 = 0x40;
    cmd_data.p2 = 0x00; //first
    //cmd_data.p2 = 0x01; //second
    cmd_data.lc = 0x07;
    cmd_data.le = 0x00;
    //cmd_data.le_type = 0x01;
    cmd_data.pdata = apdu_get_status;
    cmd_data.cpdu_type = 0x00;

    rsp_data.pdata = rdata;

    result = secEseTransmit(channel,&cmd_data,&rsp_data);
    if (result != ESESTATUS_OK){
        LOGE("getRFActivationList secEseTransmit fail %04x", result);
        //0x6310 -> cmd_data.p2 = 0x01;
        rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;
        //error
        goto error;
    } else {
        LOGD("getRFActivationList secEseTransmit success %04x", result);
    }

    if (result == ESESTATUS_OK){
        /* Note:  rpdu.len is uint16 anc can't be > MAX_DATA_SIZE */
        memcpy(rsp->data, rsp_data.pdata, rsp_data.len);
        rsp->dataLen = rsp_data.len;
        rsp->ret = RET_SUCCESS;
        LOGD("getRFActivationList1 return RET_SUCCESS");

        offset = rsp_data.len;
    }

    //send 80F24001#(4F005C#(4F9F70))00
    cmd_data.cla = 0x80;
    cmd_data.ins = 0xF2;
    cmd_data.p1 = 0x40;
    //cmd_data.p2 = 0x00; //first
    cmd_data.p2 = 0x01; //second
    cmd_data.lc = 0x07;
    cmd_data.le = 0x00;
    cmd_data.pdata = apdu_get_status;
    cmd_data.cpdu_type = 0x00;

    for(i=0; i<20; i++) {
        rsp_data.pdata = rdata;
        result = secEseTransmit(channel,&cmd_data,&rsp_data);
        if (result != ESESTATUS_OK){
            LOGE("getRFActivationList secEseTransmit fail %04x", result);
            rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;
            //error
            goto error;
        } else {
            LOGD("getRFActivationList secEseTransmit success %04x", result);
        }

        if (result == ESESTATUS_OK) {
            if(((rsp_data.sw1 == 0x90) && (rsp_data.sw2 == 0x00)) || ((rsp_data.sw1 == 0x63) && (rsp_data.sw2 == 0x10)) ) {
                if((offset + rsp_data.len) >= MAX_DATA_SIZE) {
                    LOGE("getRFActivationList Data Overflow Error : %d", (offset + rsp_data.len));
                    rsp->ret = RET_ERR_BUFFER_OVERFLOW;
                    break;
                }
                memcpy(rsp->data + offset, rsp_data.pdata, rsp_data.len);
                //rsp->dataLen = rsp_data.len;

                offset = (offset+rsp_data.len);
                rsp->dataLen = offset;

                if((rsp_data.sw1 == 0x90) && (rsp_data.sw2 == 0x00)) {
                    LOGI("getRFActivationList2 Check Finish");
                    rsp->ret = RET_SUCCESS;
                    LOGD("getRFActivationList2 return RET_SUCCESS");
                    break;
                } else if((rsp_data.sw1 == 0x63) && (rsp_data.sw2 == 0x10)) {
                    LOGI("getRFActivationList2 Check again");
                    //continue;
                }
            }
            else {
                LOGE("getRFActivationList2 Check Error");
                rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;
                break;
            }
        }
    }

error:
    if(rsp->ret == RET_SUCCESS) {
        LOGI("getRFActivationList returned: %d", rsp->ret);
    }
    else {
        LOGE("getRFActivationList returned: %d", rsp->ret);
    }

    //Close
    result = secEseClose(channel);
    if (result != ESESTATUS_OK){
        LOGE("getRFActivationList secEseClose fail %04x", result);
        //rsp->ret = RET_ERR_GET_RF_ACTIVATION_LIST;
        //goto error;
    } else {
        LOGD("getRFActivationList secEseClose success %04x", result);
    }
}

void deactivationRFAID(p_cmd_t cmd, p_rsp_t rsp) {
    secEse_7816_rpdu_t rpdu;
    secEse_7816_cpdu_t cpdu;
    uint8_t r_data[MAX_RAPDU_DATA_SIZE];
    ESESTATUS ese_status = 0;
    uint8_t scrs_aid[] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
    uint8_t cpduData[MAX_CAPDU_DATA_SIZE] = {0,};
    uint32_t cpduLen = 0, inputSize;
    uint32_t i, j = 0;
    uint8_t AIDWhiteList[][100] = {{0x4F, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}, /*PPSE*/
        {0xA0, 0x00, 0x00, 0x02, 0x20, 0x19, 0x03, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}, /*SCRS_SSD*/
        {0xA0, 0x00, 0x00, 0x02, 0x20, 0x19, 0x03, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}, /*UCM_SSD*/
        {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00}, /*M4M Desfire*/
        };
    uint32_t AIDWhiteListSize[4] = {16, 16, 16, 7};
    uint32_t AIDWhiteListCNT = 4;

    int checkWhiteList = -1;

    LOGI("Delete Default Card AID");

    memset(&cpdu, 0, sizeof(secEse_7816_cpdu_t));
    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    memset(r_data, 0, MAX_RAPDU_DATA_SIZE);

    inputSize = cmd->dataLen;
    if (inputSize > sizeof(cpduData)) {
        LOGE("cpduData overflow error");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
    memcpy(cpduData, cmd->data, inputSize);

    cpduLen = inputSize;
    rpdu.pdata = r_data;
    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    rpdu.pdata = r_data;

    //check WhiteList
    for(i=0; i<AIDWhiteListCNT; i++) {
        checkWhiteList = -1;
        if(AIDWhiteListSize[i] == cpduLen && cpduLen < 100) {
            for(j=0; j<cpduLen; j++) {
                if(AIDWhiteList[i][j] == cpduData[j]) {
                    checkWhiteList = 2;
                } else {
                    checkWhiteList = -1;
                    break;
                }
            }
        }
        if(checkWhiteList == 2) {
            rsp->ret = RET_ERR_DO_NOT_NEED_DEACTIVATE;
            goto error_whiteList;
        }
    }

    if (ESESTATUS_SUCCESS != secEseSelect(0x00, scrs_aid, 0, 0x09, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE( "Delete Card AID failed to SELECT scrs - ESESTATUS = 0x%08X", ese_status );
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
//80f00100#(4f#(AID))
    cpdu.cla = 0x80;
    cpdu.ins = 0xF0;
    cpdu.p1 = 0x01;
    cpdu.p2 = 0x00;
    cpdu.lc = cpduLen;
    cpdu.pdata = cpduData;
    cpdu.le = 0x00;

    if (ESESTATUS_SUCCESS != secEseTransmit(0x00, &cpdu, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE("Delete Card AID failed to send apdu - ESESTATUS = 0x%08X", ese_status);
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }

    /* Note:  rpdu.len is uint16 anc can't be > MAX_DATA_SIZE */
    memcpy(rsp->data, rpdu.pdata, rpdu.len);
    rsp->dataLen = rpdu.len;
    if(rpdu.len+1 > (signed)sizeof(rsp->data)) {
        LOGE("Delete Card AID failed data Overflow");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
    rsp->data[rpdu.len] = rpdu.sw1;
    rsp->data[rpdu.len+1] = rpdu.sw2;
    rsp->ret = RET_SUCCESS;

    hex_print_tag_debug("Delete default Card :: ", rsp->data, rsp->dataLen+2);

    LOGD("Delete default Card result : 0x%02x %02x", rpdu.sw1, rpdu.sw2);

error_deleteCard:
    LOGE("Delete default Card returned: %d", rsp->ret);

error_whiteList:
    LOGE("White List : %d", rsp->ret);
}

void deactivationRFAIDDebug(p_cmd_t cmd, p_rsp_t rsp) {
    secEse_7816_rpdu_t rpdu;
    secEse_7816_cpdu_t cpdu;
    uint8_t r_data[MAX_RAPDU_DATA_SIZE];
    ESESTATUS ese_status = 0;
    uint8_t scrs_aid[] = {0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
    uint8_t cpduData[MAX_CAPDU_DATA_SIZE] = {0,};
    uint32_t cpduLen = 0, inputSize;
    uint32_t i, j = 0;
    uint8_t AIDWhiteList[][100] = {{0x4F, 0x08, 0xA0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00, 0x00}, /*ISD*/
        {0x4F, 0x0A, 0xA9, 0xA8, 0xA7, 0xA6, 0xA5, 0xA4, 0xA3, 0xA2, 0xA1, 0xA0}, /*AMSD*/
        {0x4F, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31}, /*PPSE*/
        {0x4F, 0x09, 0xA0, 0x00, 0x00, 0x01, 0x51, 0x41, 0x43, 0x4C, 0x01},/*ARA-C*/
        {0xA0, 0x00, 0x00, 0x02, 0x20, 0x19, 0x03, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}, /*SCRS_SSD*/
        {0xA0, 0x00, 0x00, 0x02, 0x20, 0x19, 0x03, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}, /*UCM_SSD*/
        {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00}, /*M4M Desfire*/
        };
    uint32_t AIDWhiteListSize[7] = {10, 12, 16, 11, 16, 16, 7};
    uint32_t AIDWhiteListCNT = 7;

    int checkWhiteList = -1;

    LOGI("Delete Default Card AID");

    memset(&cpdu, 0, sizeof(secEse_7816_cpdu_t));
    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    memset(r_data, 0, MAX_RAPDU_DATA_SIZE);

    inputSize = cmd->dataLen;
    if (inputSize > sizeof(cpduData)) {
        LOGE("cpduData overflow error");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
    memcpy(cpduData, cmd->data, inputSize);

    cpduLen = inputSize;
    rpdu.pdata = r_data;
    memset(&rpdu, 0, sizeof(secEse_7816_rpdu_t));
    rpdu.pdata = r_data;

    //check WhiteList
    for(i=0; i<AIDWhiteListCNT; i++) {
        checkWhiteList = -1;
        if(AIDWhiteListSize[i] == cpduLen && cpduLen < 100) {
            for(j=0; j<cpduLen; j++) {
                if(AIDWhiteList[i][j] == cpduData[j]) {
                    checkWhiteList = 2;
                } else {
                    checkWhiteList = -1;
                    break;
                }
            }
        }
        if(checkWhiteList == 2) {
            rsp->ret = RET_ERR_DO_NOT_NEED_DEACTIVATE;
            goto error_whiteList;
        }
    }

    if (ESESTATUS_SUCCESS != secEseSelect(0x00, scrs_aid, 0, 0x09, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE( "Delete Card AID failed to SELECT scrs - ESESTATUS = 0x%08X", ese_status );
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
//80f00100#(4f#(AID))
    cpdu.cla = 0x80;
    cpdu.ins = 0xF0;
    cpdu.p1 = 0x01;
    cpdu.p2 = 0x00;
    cpdu.lc = cpduLen;
    cpdu.pdata = cpduData;
    cpdu.le = 0x00;

    if (ESESTATUS_SUCCESS != secEseTransmit(0x00, &cpdu, &rpdu) || rpdu.sw1 != 0x90 || rpdu.sw2!= 0x00) {
        LOGE("Delete Card AID failed to send apdu - ESESTATUS = 0x%08X", ese_status);
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }

    /* Note:  rpdu.len is uint16 anc can't be > MAX_DATA_SIZE */
    memcpy(rsp->data, rpdu.pdata, rpdu.len);
    rsp->dataLen = rpdu.len;
    if(rpdu.len+1 > (signed)sizeof(rsp->data)) {
        LOGE("Delete Card AID failed data Overflow");
        rsp->ret = RET_ERR_DELETE_WALLET;
        goto error_deleteCard;
    }
    rsp->data[rpdu.len] = rpdu.sw1;
    rsp->data[rpdu.len+1] = rpdu.sw2;
    rsp->ret = RET_SUCCESS;

    hex_print_tag_debug("Delete default Card :: ", rsp->data, rsp->dataLen+2);

    LOGD("Delete default Card result : 0x%02x %02x", rpdu.sw1, rpdu.sw2);

error_deleteCard:
    LOGE("Delete default Card returned: %d", rsp->ret);

error_whiteList:
    LOGE("White List : %d", rsp->ret);
}

