/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_18_win32.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to use the share memory in
*            SenseLock EL EXF.
*
*            
*  History:        
*  --------------------------------------------------------        
*            10/28/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/

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

#include "..\..\inc\win32\sense4.h"
#include "..\..\inc\win32\crypt2.h"
#include "..\..\inc\win32\ses_err_msg.h"/* for debug purpose only */
#include "..\..\common\common.h"

#include "..\inc\sample_18.h"


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

IO_PACKAGE stDataPkgIn = {0};
IO_PACKAGE stDataPkgOut = {0};

#define S4_EXE_FILE_ID "ef18"

/* for debug purpose only : handle results returned by stDataPkgOut.tag */
#ifdef _DEBUG
void HandleError(unsigned char bErrCode, unsigned char *pbErrInfo)
{
	switch(bErrCode)
	{
	case ERR_SUCCESS:
		printf("Success!\n");
		break;
	case ERR_SES:
		printf("Error during SES Execution :\n%s\n", SesErrMsgs[*pbErrInfo]);
		break;
	case ERR_INVALID_PARAMETER:
		printf("Invalid input,pls check your input data\n");
		break;
	default:
		printf("Undefined error code!\n");
		break;
	}
	return;
}
#else
void HandleError(unsigned char bErrCode, unsigned char *pbErrInfo)
{
	return;
}	
#endif

/* check result for S4Execute(...) */
unsigned long CheckS4ExecutionResult(unsigned long dwResult,unsigned long dwBytesReturned,unsigned char tag)
{
	
	if (dwResult != S4_SUCCESS) 
	{
		printf("S4 Execution failed! <error code: 0x%08x>\n", dwResult);
		return 0;
	}
	
	if (dwBytesReturned < 2)
	{
		printf("Invalid data package returned!\n");
		return 0;
	}
	
	if (tag != ERR_SUCCESS)
	{
		HandleError(stDataPkgOut.tag, stDataPkgOut.buff);
		return 0;
	}
	return 1;
}


int main(int argc, char* argv[])
{
	unsigned char lpUserPin[8] = "12345678";
	int i = 0;
	int total = 0;	
    if (!(dwResult = OpenS4ByIndex(FIRST_S4_INDEX,&stS4Ctx)))
	{
		return 1;
	}   
	dwResult = S4ChangeDir(&stS4Ctx, "\\");
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change directory failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	
	// Call S4VerifyPin(...) to verify User PIN so as to get the privilege to execute the program in SenseLock EL.
	dwResult = S4VerifyPin(&stS4Ctx, lpUserPin, sizeof(lpUserPin), S4_USER_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Verify Pin failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	
	stDataPkgIn.tag = CMD_RESET; /* initiate the share memory */
	stDataPkgIn.len = 0;
	printf("Call SenseLock EL EXF to initiate the share memory...\n");
	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, &stDataPkgIn, 
						IO_PACKAGE_HEADER_SIZE+stDataPkgIn.len, 
						&stDataPkgOut, sizeof(IO_PACKAGE), &dwBytesReturned);
	if (!CheckS4ExecutionResult(dwResult, dwBytesReturned, stDataPkgOut.tag))
	{
		ResetAndCloseS4(&stS4Ctx);
		return 1;
	}
	printf("Success!\n");

	
	printf("Call SenseLock EL EXF to do the 1st count...\n");
	stDataPkgIn.tag = CMD_COUNT;

	stDataPkgIn.len = 5;
	stDataPkgIn.buff[0] = 99;
	stDataPkgIn.buff[1] = 87;
	stDataPkgIn.buff[2] = 65;
	stDataPkgIn.buff[3] = 55;
	stDataPkgIn.buff[4] = 60;
	
	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, &stDataPkgIn, 
				IO_PACKAGE_HEADER_SIZE+stDataPkgIn.len, 
				&stDataPkgOut, sizeof(IO_PACKAGE), &dwBytesReturned);
	if (!CheckS4ExecutionResult(dwResult, dwBytesReturned, stDataPkgOut.tag))
	{
		ResetAndCloseS4(&stS4Ctx);
		return 1;
	}
	printf("Success!\n");
		
	printf("Call SenseLock EL EXF to do the 2nd count...\n");
	stDataPkgIn.tag = CMD_COUNT;
	stDataPkgIn.len = 5;
	stDataPkgIn.buff[0] = 88;
	stDataPkgIn.buff[1] = 82;
	stDataPkgIn.buff[2] = 67;
	stDataPkgIn.buff[3] = 78;
	stDataPkgIn.buff[4] = 76;
	
	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, &stDataPkgIn, 
				IO_PACKAGE_HEADER_SIZE+stDataPkgIn.len, 
				&stDataPkgOut, sizeof(IO_PACKAGE), &dwBytesReturned);
	if (!CheckS4ExecutionResult(dwResult, dwBytesReturned, stDataPkgOut.tag))
	{
		ResetAndCloseS4(&stS4Ctx);
		return 1;
	}
	printf("Success!\n");	
	
	printf("Call SenseLock EL EXF to do the 2nd calculation...\n");

	stDataPkgIn.tag = CMD_GET;
	stDataPkgIn.len = 0;
	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, &stDataPkgIn, 
			IO_PACKAGE_HEADER_SIZE+stDataPkgIn.len, 
			&stDataPkgOut, sizeof(IO_PACKAGE), &dwBytesReturned);
	if (!CheckS4ExecutionResult(dwResult, dwBytesReturned, stDataPkgOut.tag))
	{
		ResetAndCloseS4(&stS4Ctx);
		return 1;
	}
	printf("Success!\n");
	for (i=0; i<stDataPkgOut.len; i++)
	{
		total+=stDataPkgOut.buff[i];
	}
	printf("Total numbers:%d\n", total);
	printf("Scores greater than 90  :<number:%d> <percentage:%d %%>\n", stDataPkgOut.buff[0], stDataPkgOut.buff[0]*100/total);
	printf("Scores between 80 and 89:<number:%d> <percentage:%d %%>\n", stDataPkgOut.buff[1], stDataPkgOut.buff[1]*100/total);
	printf("Scores between 60 and 79:<number:%d> <percentage:%d %%>\n", stDataPkgOut.buff[2], stDataPkgOut.buff[2]*100/total);
	printf("Scores less than 60     :<number:%d> <percentage:%d %%>\n", stDataPkgOut.buff[3], stDataPkgOut.buff[3]*100/total);
	
	
	/* for better security,use the following instead of using S4close() directly */
 	ResetAndCloseS4(&stS4Ctx);
	getch();

	return dwResult;
}
