#include <time.h>
#include <stdio.h>
#include <string.h>

#include "arg.h"
#include "common.h"
#include "crd_ut10xx.h"

int
main(int argc, const char **argv)
{
	const char *help = 
		"\n\tspeed [-help] [-devnum=num] [-tests=num] -upin=pin\n"
		"\t-help\n"
		"\t\tPrint this information.\n\n"
		"\t-devnum\n"
		"\t\tSpecifies number of device which should be initialised.\n"
		"\t\tNumbers of devices can be foun calling info program.\n"
		"\t\tDefault value of this number is equal 0 and correspond to\n"
		"\t\tthe first device attached to host.\n\n"
		"\t-tests\n"
		"\t\tNumber of tests, that should be done. Default value of\n"
		"\t\tthis is equal 10.\n\n"
		"\t-upin\n"
		"\t\tSpecifies User PIN.\n\n";
	
	CRD_UT10XX_t *ut;
	
	unsigned char sign[128];
	
	int devnum = 0;
	int tests = 1;
	const char *upin = 0;
	
	time_t t1;
	time_t t2;
	
	int status;
	int i;
	
	if ( prologue(argv, help) )
		return (-1);
	
	if ( arg_getall(argv, "ii!s", "-devnum", &devnum, "-tests", &tests, 
		"-upin", &upin) < 0 )
		return (-2);
	
	if ( (ut = CRD_UT10XX_find_by_index(devnum)) == 0 )
	{
		epilogue(stderr, "there is no device of this number, use 'info'"
			" to check\n", 0, 0);
		return (-3);
	}
	
	CRD_UT10XX_session_open(ut);
	
	t1 = time(0);
	for (i = 0; i < tests; i++)
	{
		if ( (status = CRD_UT10XX_cmd_ecdsa_sign(ut, upin, 0, (char *)sign, 
			sizeof(sign), "12345678901234567890", 20)) < 0 )
		{
			epilogue(stderr, 0, status, ut);
			return (status);
		}
	}
	t2 = time(0);
	
	printf("time %0.2f s.\n", difftime(t2, t1) / tests);
	
	CRD_UT10XX_session_close(ut);

	return (epilogue(stderr, 0, 0, 0));
}
