#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "sense4.h"

/*bubble sort function removed. Add SenseLock EL invoking code. */
void call_sense4(char *, unsigned char *, int);

/*main procedure*/
void main()
{
	unsigned char test[] = {4,3,8,2,9,7,1,5,0,6};
	int len = sizeof(test);
	int i;

	call_sense4("d001", test, len);

	printf("result:\n");
	for (i=0;i<len;i++)
	{
		printf("%d ",test[i]);
 	}
}

void call_sense4(char *fid, unsigned char *buff, int len)
{
	SENSE4_CONTEXT ctx = {0};
	SENSE4_CONTEXT *pctx = NULL;
	unsigned long size = 0;
	unsigned long ret = 0;
	
	S4Enum(pctx, &size);
	if (size == 0)
	{
		printf("SenseLock EL not found!\n");
		return;
	}
	
	pctx = (SENSE4_CONTEXT *)malloc(size);
	if (pctx == NULL)
	{
		printf("Not enough memory!\n");
		return;
	}
	
	ret = S4Enum(pctx, &size);
	if (ret != S4_SUCCESS)
	{
		printf("Enumerate SenseLock EL error!\n");
		free(pctx);
		return;
	}
	
	memcpy(&ctx, pctx, sizeof(SENSE4_CONTEXT));
	free(pctx);
	pctx = NULL;
	
	ret = S4Open(&ctx);
	if (ret != S4_SUCCESS)
	{
		printf("Open SenseLock EL failed!\n");
		return;
	}
	
	ret = S4ChangeDir(&ctx, "\\");
	if (ret != S4_SUCCESS)
	{
		printf("No root directory found!\n");
		S4Close(&ctx);
		return;
	}
	
	ret = S4VerifyPin(&ctx, "12345678", 8, S4_USER_PIN);
	if (ret != S4_SUCCESS)
	{
		printf("Verify user PIN failed!\n");
		S4Close(&ctx);
		return;
	}
	
	ret = S4Execute(&ctx, fid, buff, len, buff, len, &size);
	if (ret != S4_SUCCESS)
	{
		printf("Execute SenseLock EL exe failed!\n");
		S4Close(&ctx);
		return;
	}
	
	S4Close(&ctx);
	
	return;
}
