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

/* global variables definition */
SENSE4_CONTEXT stS4Ctx = {0};
unsigned long dwResult = 0;
unsigned long dwBytesReturned = 0;

#define S4_EXE_FILE_ID "ef19"

int main(int argc, char* argv[])
{
	unsigned char lpUserPin[] = "12345678";
	unsigned char test[8] = {3,1,8,2,5,4,7,6};

	//デバイスに接続
    if (!(dwResult = OpenS4ByIndex(FIRST_S4_INDEX,&stS4Ctx)))
	{
		return 1;
	}   
	
	//ルートディレクトリへ移動
	dwResult = S4ChangeDir(&stS4Ctx, "\\");
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change directory failed! \n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	
	// プログラムを実行するには ユーザPIN でログインする必要がある
	dwResult = S4VerifyPin(&stS4Ctx, lpUserPin, strlen((LPCSTR)lpUserPin), S4_USER_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Verify Pin failed! \n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}

	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, test, sizeof(test), test, sizeof(test), &dwBytesReturned);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Execute program in Elite EL failed! \n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");

	printf("Data after being bubble-ordered:");
	for( int i=0 ; i < sizeof(test); i++)
		printf("%d ", test[i]);

	/* ユーザPINでログイン中のため、デイバイスをリセットして閉じる */
	ResetAndCloseS4(&stS4Ctx);

	return dwResult;
}