// CreateDir.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sense4.h"
#include <stdio.h>
#include "AccKey.h"


int main(int argc, char* argv[])
{
	SENSE4_CONTEXT * pCtx = NULL;
	DWORD			dwRet;

	/*Assume the current DEV PIN is "123456781234567812345678"*/
	BYTE			pPin[24]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
								0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
								0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};


	//Access key
	pCtx = AccessKey();
	if(pCtx == NULL)
		printf("Accessed device failed.");
	
	//If root directory exists, switch current directory to root directory
	dwRet = S4ChangeDir(pCtx, "\\");

	//Verify DEV PIN
	dwRet=S4VerifyPin(pCtx,pPin,24,S4_DEV_PIN);
	S4CREATEDIRINFO CreateDirInfo;
	CreateDirInfo.dwS4CreateDirInfoSize = sizeof(S4CREATEDIRINFO);
	memcpy(CreateDirInfo.szAtr, "Sense   ", 8);
	//Create MF, set ID to "Sense   "
	dwRet=S4CreateDirEx(pCtx,"\\",0,S4_CREATE_ROOT_DIR, &CreateDirInfo);
	if(dwRet == S4_SUCCESS)
		printf("Created MF success.\n");
	else
	{
		printf("Created MF failed.\n");

		//Failed to create MF, the possible cause can be non-empty device.
		//Invoke S4EraseDir to delete file system first, then recreate MF
		dwRet=S4EraseDir(pCtx, NULL);

		dwRet=S4CreateDir(pCtx,"\\",0,S4_CREATE_ROOT_DIR);
		if(dwRet == S4_SUCCESS)
			printf("Created MF again , success.\n");
		else
			printf("Created MF again , failed.\n");
	}


	//Change current directory to MF
	dwRet=S4ChangeDir(pCtx,"\\");

	//Verify DEV PIN
	dwRet=S4VerifyPin(pCtx,pPin,24,S4_DEV_PIN);

	//Create child directory for local device with S4_CREATE_SUB_DIR flag
	dwRet=S4CreateDir(pCtx,"0001",2000,S4_CREATE_SUB_DIR);

	//Create module for net device with S4_CREATE_MODULE flag, only the first level child directory can be created as module.
	dwRet=S4CreateDir(pCtx,"0002",3000,S4_CREATE_MODULE);

	//Change current directory to 0001
	dwRet=S4ChangeDir(pCtx,"0001");

	//Verify DEV PIN of DF1
	dwRet=S4VerifyPin(pCtx, pPin, 24, S4_DEV_PIN);	

	//Create child directory named "0003" in  "0001"
	dwRet=S4CreateDir(pCtx,"0003",1000,S4_CREATE_SUB_DIR);

	//Change current directory to \0001\0003
	dwRet=S4ChangeDir(pCtx,"\\0001\\0003");

	// ....did something......//

	//Change current directory to \0001
	dwRet=S4ChangeDir(pCtx,"\\0001");

	//Verify DEV PIN
	dwRet=S4VerifyPin(pCtx,pPin,24,S4_DEV_PIN);

	//Erase the content of 0001, the directory specified by "\0001\0003" does NOT exist now,
	//but the direcotry "\0001" does exist.
	dwRet = S4EraseDir(pCtx,NULL);

	//.......did something......//

	//close device
	S4Close(pCtx);

	//free memory
	free(pCtx);
	pCtx = NULL;
	return 0;
}
