/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_10.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to handle a normal SenseLock EL file.
*            
*            
*  History:        
*  --------------------------------------------------------        
*            11/08/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/

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

#include "..\..\inc\win32\sense4.h"
#include "..\..\inc\win32\PSense4.h"

#include "..\..\common\common.h"

#define S4_EXE_FILE_ID "ef10"


int main(int argc, char* argv[])
{
	SENSE4_CONTEXT stS4Ctx = {0};
	unsigned long dwResult = 0;

	unsigned char lpPin[] = "123456781234567812345678";
	unsigned long dwPinLen = sizeof(lpPin)-1;
	
	unsigned char lpUsrPin[] = "12345678";
	unsigned long dwUsrPinLen = sizeof(lpUsrPin)-1;
	
	unsigned long dwFileSize = 3000;/* estimated file size: 3K */
	unsigned long dwBytesWritten = 0;

	unsigned char test[8] = {3,1,8,2,5,4,7,6};
	unsigned long dwBytesReturned = 0;

	EFINFO stEFInfo = {0};
	
	printf("Open first SenseLock EL device...\n");
	if (!(dwResult = OpenS4ByIndex(FIRST_S4_INDEX, &stS4Ctx)))
	{
		return 1;
	} 
	printf("Success!\n");
	
	/* to make sure you are in the root dir */
	printf("Change to root directory...\n");
	dwResult = S4ChangeDir(&stS4Ctx, "\\");
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change directory failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");

	dwResult = S4VerifyPin(&stS4Ctx, lpPin, dwPinLen, S4_DEV_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Verify DEV_PIN failed! <error code: 0x%08x>\n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}
	
	printf("Write hex file to SenseLock EL device....\n");
	dwFileSize = 0;
	/* use S4_HEX_FILE instead of S4_EXE_FILE if you want to write hex file to device directly*/
	dwResult = PS4WriteFile(&stS4Ctx, S4_EXE_FILE_ID, "sample_10_hw.hex", &dwFileSize, 
		S4_CREATE_NEW | S4_FILE_EXECUTE_ONLY, S4_HEX_FILE, &dwBytesWritten);
	if ((dwResult != S4_SUCCESS) && (dwResult != S4_FILE_EXIST))
	{
		printf("Write file to device failed! <error code: 0x%08x>\n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}
	else if (dwResult == S4_FILE_EXIST)
	{
		printf("File of the same file ID already exist! Overwritting...!\n");
		dwFileSize = 0;
		dwResult = PS4WriteFile(&stS4Ctx, S4_EXE_FILE_ID, "sample_10_hw.hex", &dwFileSize, 
			S4_UPDATE_FILE, S4_HEX_FILE, &dwBytesWritten);
		if (dwResult != S4_SUCCESS)
		{
			printf("Update file failed! <error code: 0x%08x>\n",dwResult);
			ResetAndCloseS4(&stS4Ctx);
			return dwResult;			
		}
		printf("Success!\n");
	}
	else
	{
		printf("Success!\n");
	}
	
    printf("Get just-written-in file information from device...\n");
	dwResult = S4Control(&stS4Ctx, S4_EF_INFO, S4_EXE_FILE_ID, 4, &stEFInfo, sizeof(EFINFO), &dwBytesReturned);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Get file information failed! <error code: 0x%08x>\n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}
	printf("Success! EfID = 0x%04x; EfSize = 0x%04x\n", stEFInfo.EfID, stEFInfo.EfSize);
	
	
    
	dwResult = S4VerifyPin(&stS4Ctx, lpUsrPin, dwUsrPinLen, S4_USER_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Verify USER_PIN failed! <error code: 0x%08x>\n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}

    printf("Data to be bubble-ordered:");
	hexprint(stdout, test, sizeof(test));

	printf("Execute program in SenseLock EL...\n");
	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, test, sizeof(test), test, sizeof(test), &dwBytesReturned);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Execute program in SenseLock EL failed! <error code: 0x%08x>\n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");
    printf("Data after being bubble-ordered:");
	hexprint(stdout, test, sizeof(test));
	
	ResetAndCloseS4(&stS4Ctx);
    getch(); 
	return dwResult;
}
