#include "shared/Close_Secure_Channel.h"
#include "dk_scp_common.h"
#include "dk_channel.h"

DK_Result process_close_secure_channel(tl_dk_ctx_t *ctx, tz_dk_close_secure_channel_payload_t *sendmsg, tz_dk_close_secure_channel_payload_t *respmsg)
{
    DK_Result rc;

    // TODO: should we send a command to the eSE telling it that the
    // session is closed or just reset the session in the OCE side?
    // A Open Secure Channel command is necessary for reestabilishing
    // a channel, and it triggers a reset in the eSE side, so it might
    // not be necessary to force one at this point.

    if (ctx->scp_ctx.session_state != SESSION_CLOSED) {
        scp_clear(&ctx->scp_ctx);
    }

    if (ctx->channel.is_open) {
        close_channel(&ctx->channel);
    }

    rc = DK_SUCCESS;

catch_error:
    return rc;
}