#ifndef DK_CMD_GET_RESPONSE_H
#define DK_CMD_GET_RESPONSE_H

#include <limits.h>
#include <sec_apdu.h>

#include "dk_common.h"
#include "dk_constants.h"
#include "dk_log.h"
#include "dk_constants.h"
#include "dk_apdu.h"
#include "dk_channel.h"

/**
 * Generate a GET RESPONSE APDU.
 * @param cpdu command APDU structure to be filled.
 * @param cpdu_len final length of cpdu after filled.
 * 
 * @return DK_SUCCESS if successfully filled cpdu; DK_ERROR_GENERIC otherwise.
 */
DK_Result generate_get_response_cpdu(byte* buf, size_t* buflen);

/**
 * Validate rpdu status bytes and copy rpdu->pdata to response_data
 * 
 * @param rpdu response APDU structure sent by eSE.
 * @param response_data response data being constructed over multiple transmissions. 
 *        Either partial if communication still in progress or full if over.
 * @param response_data_len current response_data length.
 * 
 */
DK_Result handle_get_response_response(secEse_7816_rpdu_t* rpdu, byte* response_data, size_t* response_data_len);

/**
 * Handle responses that may come in multiple APDUs received
 * over channel c.
 * 
 * @param rpdu first response rpdu.
 * @param response_data byte array to which all received data will be written to.
 *        Must have enough memory to receive all data.
 * @param response_data_len current length of response_data.
 * @param c channel over which transmission occurs.
 * 
 * @return DK_SUCCESS on SUCCESS; different value on error;
 */
DK_Result handle_fragmented_response(
    secEse_7816_rpdu_t *rpdu,
    byte *response_data,
    size_t *response_data_len,
    channel_t* channel);

#endif