/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_9.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to operate SenseLock EL directories.
*            
*            
*  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 DEST_DIR "df01"

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

	unsigned char lpOldPin[] = "123456781234567812345678";
	unsigned long dwOldPinLen = sizeof(lpOldPin)-1;
	unsigned char lpNewPin[] = "zaq12wsxcde34rfvbgt56yhn";
	unsigned long dwNewPinLen = sizeof(lpNewPin)-1;

	unsigned char lpOldUsrPin[] = "12345678";
	unsigned long dwOldUsrPinLen = sizeof(lpOldUsrPin)-1;
	unsigned char lpNewUsrPin[] = "zaq12wsx";
	unsigned long dwNewUsrPinLen = sizeof(lpNewUsrPin)-1;
	
	unsigned long dwSubDirSize = 20000;/* estimated directory size: 3K */

	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");

	
	printf("Change directory to %s...\n", DEST_DIR);
	dwResult = S4ChangeDir(&stS4Ctx, DEST_DIR);
	if (dwResult != S4_SUCCESS && dwResult != S4_FILE_NOT_FOUND) 
	{
		printf("Change directory failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}else if (dwResult == S4_SUCCESS)
	{
		printf("Success!\n");
		/* directory already exist,erase the dir so as to erase all the file under the dir */
		printf("To erase all the file under %s,first verify DEV_PIN...\n", DEST_DIR);
		dwResult = S4VerifyPin(&stS4Ctx, lpNewPin, dwNewPinLen, S4_DEV_PIN);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Verify DEV_PIN failed! <error code: 0x%08x>\n",dwResult);
			S4Close(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
		printf("Erasing...\n");
		/* Important! after the dir being erased, the PIN has been set to the default value */
		dwResult = S4EraseDir(&stS4Ctx, NULL);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Erase root directory failed! <error code: 0x%08x>\n",dwResult);
			ResetAndCloseS4(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
	}else /* create a directory named "df01"*/
	{
		printf("Not exist!Now create directory %s...\n", DEST_DIR);
		
		/* need to verify DEV_PIN of root directory first */
		printf("Verify DEV_PIN of root directory...\n");
		dwResult = S4VerifyPin(&stS4Ctx, lpOldPin, dwOldPinLen, S4_DEV_PIN);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Verify DEV_PIN failed! <error code: 0x%08x>\n",dwResult);
			S4Close(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
		printf("Create directory %s ...\n", DEST_DIR);
		dwResult = S4CreateDir(&stS4Ctx, DEST_DIR, dwSubDirSize, S4_CREATE_SUB_DIR);
		if (dwResult != S4_SUCCESS) 
		{
			printf("Create df01 directory failed! <error code: 0x%08x>\n",dwResult);
			ResetAndCloseS4(&stS4Ctx);
			return dwResult;
		}
		printf("Success!\n");
		
		
	}
	/* change DEV_PIN and USR_PIN after directory creation or erasion,that's important! */
	printf("Change USER_PIN of directory %s...\n", DEST_DIR);
	dwResult = S4ChangePin(&stS4Ctx,lpOldUsrPin,dwOldUsrPinLen,lpNewUsrPin,dwNewUsrPinLen,S4_USER_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change USER_PIN of df01 directory failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");
	
	printf("Change DEV_PIN of directory %s...\n", DEST_DIR);
	dwResult = S4ChangePin(&stS4Ctx,lpOldPin,dwOldPinLen,lpNewPin,dwNewPinLen,S4_DEV_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change DEV_PIN of df01 directory failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");

	

	ResetAndCloseS4(&stS4Ctx);
    getch(); 
	return dwResult;
}





















