#include "tests.h"
#include "tests_cmd.h"
#include "common.h"

int
TEST_prepare_device(CRD_UT10XX_t *ut, TEST_conf_t *conf)
{
	unsigned char out[2] = {HF_CMD, TCMD_TEST_INFO};
	unsigned char in[4];
	int status;
	int test_base;
	int params_id;
	
	TEST_session_open(ut);
	status = TEST_session_wr_rd(ut, out, sizeof(out), in, sizeof(in));
	
	if (in[0] != DF_ERR)
	{
		params_id = ((int)in[2] << 8) | (int)in[3];
		ut->info.ecc_params = CRD_UT10XX_get_ecc_params(params_id);
	}
	else
	{
		fprintf(stderr, "device error %s\n", CRD_UT10XX_lib_error_desc(ERR_CRD_UT10XX_DEV - in[1]));
	}
	
	TEST_session_close(ut);
	
	return (status);
}

int 
TEST_session_open(CRD_UT10XX_t *ut)
{
	return ((*ut->open)(ut));
}

int 
TEST_session_wr_rd(CRD_UT10XX_t *ut, const unsigned char *out, int out_octets, 
	unsigned char *in, int in_octets)
{
	int status;
	
	if ( (status = (*ut->write)(ut, out, out_octets)) < 0 )
	{
		(*ut->close)(ut);
		return (status);
	}
	
	if ( (status = (*ut->read)(ut, in, in_octets)) < 0 )
	{
		(*ut->close)(ut);
		return (status);
	}
	
	return (status);
}

int 
TEST_session_close(CRD_UT10XX_t *ut)
{
	return ((*ut->close)(ut));
}
