/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_35_win32.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to get time from
*            SenseLock EL with real-clock function.
*            
*  History:        
*  --------------------------------------------------------        
*            10/28/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/

#include <stdio.h>
#include <conio.h>
#include <assert.h>
#include <time.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"

/* RTC (Real-Time Clock) format:quoted from "ses_v3.h":for being used in win32 program	*/
typedef struct _RTC_TIME_T 
{
	BYTE	second;								/* second (0-59)					*/
	BYTE	minute;								/* minute (0-59)					*/
	BYTE	hour;								/* hour (0-23)						*/
	BYTE	day;								/* day of month (1-31)				*/
	BYTE	week;								/* day of week (0-6, sunday is 0)	*/
	BYTE	month;								/* month (0-11)						*/
	WORD	year;								/* year (0- 138, 1900 - 2038)		*/
} RTC_TIME_T;

#include "..\inc\sample_35.h"


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

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

#define S4_EXE_FILE_ID "ef35"

/* 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";
    DATA_BLOCK *pstDataBlk = NULL;	
	struct tm tmp_tm = {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 = 0; /* no meaning */
	stDataPkgIn.len = 0; /* no input data */
	printf("Start to get time from SenseLock EL's internal clock ...\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");
	pstDataBlk = (DATA_BLOCK *)stDataPkgOut.buff;
	printf("Time get from SenseLock EL in time_t format :%d\n",pstDataBlk->tm);
	
    tmp_tm.tm_hour = pstDataBlk->rtc_tm.hour;
	tmp_tm.tm_mday = pstDataBlk->rtc_tm.day;
	tmp_tm.tm_min  = pstDataBlk->rtc_tm.minute;
	tmp_tm.tm_mon  = pstDataBlk->rtc_tm.month;
	tmp_tm.tm_sec  = pstDataBlk->rtc_tm.second;
	tmp_tm.tm_wday = pstDataBlk->rtc_tm.week;
	tmp_tm.tm_year = pstDataBlk->rtc_tm.year;
	
	printf("Time get from SenseLock EL in coordinated universal format is:\n %s\n", asctime(&tmp_tm));

	/* for better security,use the following instead of using S4close() directly */
	ResetAndCloseS4(&stS4Ctx);
	getch();

	return dwResult;
}
